Branch data Line data Source code
1 : : /* Report error messages, build initializers, and perform
2 : : some front-end optimizations for C++ compiler.
3 : : Copyright (C) 1987-2025 Free Software Foundation, Inc.
4 : : Hacked by Michael Tiemann (tiemann@cygnus.com)
5 : :
6 : : This file is part of GCC.
7 : :
8 : : GCC is free software; you can redistribute it and/or modify
9 : : it under the terms of the GNU General Public License as published by
10 : : the Free Software Foundation; either version 3, or (at your option)
11 : : any later version.
12 : :
13 : : GCC is distributed in the hope that it will be useful,
14 : : but WITHOUT ANY WARRANTY; without even the implied warranty of
15 : : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 : : GNU General Public License for more details.
17 : :
18 : : You should have received a copy of the GNU General Public License
19 : : along with GCC; see the file COPYING3. If not see
20 : : <http://www.gnu.org/licenses/>. */
21 : :
22 : :
23 : : /* This file is part of the C++ front end.
24 : : It contains routines to build C++ expressions given their operands,
25 : : including computing the types of the result, C and C++ specific error
26 : : checks, and some optimization. */
27 : :
28 : : #include "config.h"
29 : : #include "system.h"
30 : : #include "coretypes.h"
31 : : #include "cp-tree.h"
32 : : #include "stor-layout.h"
33 : : #include "varasm.h"
34 : : #include "intl.h"
35 : : #include "gcc-rich-location.h"
36 : : #include "target.h"
37 : :
38 : : static tree
39 : : process_init_constructor (tree type, tree init, int nested, int flags,
40 : : tsubst_flags_t complain);
41 : :
42 : :
43 : : /* Print an error message stemming from an attempt to use
44 : : BASETYPE as a base class for TYPE. */
45 : :
46 : : tree
47 : 26 : error_not_base_type (tree basetype, tree type)
48 : : {
49 : 26 : if (TREE_CODE (basetype) == FUNCTION_DECL)
50 : 0 : basetype = DECL_CONTEXT (basetype);
51 : 26 : error ("type %qT is not a base type for type %qT", basetype, type);
52 : 26 : return error_mark_node;
53 : : }
54 : :
55 : : tree
56 : 1228 : binfo_or_else (tree base, tree type)
57 : : {
58 : 1228 : tree binfo = lookup_base (type, base, ba_unique,
59 : : NULL, tf_warning_or_error);
60 : :
61 : 1228 : if (binfo == error_mark_node)
62 : : return NULL_TREE;
63 : 1228 : else if (!binfo)
64 : 0 : error_not_base_type (base, type);
65 : : return binfo;
66 : : }
67 : :
68 : : /* According to ARM $7.1.6, "A `const' object may be initialized, but its
69 : : value may not be changed thereafter. */
70 : :
71 : : void
72 : 155 : cxx_readonly_error (location_t loc, tree arg, enum lvalue_use errstring)
73 : : {
74 : :
75 : : /* This macro is used to emit diagnostics to ensure that all format
76 : : strings are complete sentences, visible to gettext and checked at
77 : : compile time. */
78 : :
79 : : #define ERROR_FOR_ASSIGNMENT(LOC, AS, ASM, IN, DE, ARG) \
80 : : do { \
81 : : switch (errstring) \
82 : : { \
83 : : case lv_assign: \
84 : : error_at (LOC, AS, ARG); \
85 : : break; \
86 : : case lv_asm: \
87 : : error_at (LOC, ASM, ARG); \
88 : : break; \
89 : : case lv_increment: \
90 : : error_at (LOC, IN, ARG); \
91 : : break; \
92 : : case lv_decrement: \
93 : : error_at (LOC, DE, ARG); \
94 : : break; \
95 : : default: \
96 : : gcc_unreachable (); \
97 : : } \
98 : : } while (0)
99 : :
100 : : /* Handle C++-specific things first. */
101 : :
102 : 155 : if (VAR_P (arg)
103 : 15 : && DECL_LANG_SPECIFIC (arg)
104 : 0 : && DECL_IN_AGGR_P (arg)
105 : 155 : && !TREE_STATIC (arg))
106 : 0 : ERROR_FOR_ASSIGNMENT (loc,
107 : : G_("assignment of constant field %qD"),
108 : : G_("constant field %qD used as %<asm%> output"),
109 : : G_("increment of constant field %qD"),
110 : : G_("decrement of constant field %qD"),
111 : : arg);
112 : 155 : else if (INDIRECT_REF_P (arg)
113 : 35 : && TYPE_REF_P (TREE_TYPE (TREE_OPERAND (arg, 0)))
114 : 184 : && (VAR_P (TREE_OPERAND (arg, 0))
115 : 16 : || TREE_CODE (TREE_OPERAND (arg, 0)) == PARM_DECL))
116 : 13 : ERROR_FOR_ASSIGNMENT (loc,
117 : : G_("assignment of read-only reference %qD"),
118 : : G_("read-only reference %qD used as %<asm%> output"),
119 : : G_("increment of read-only reference %qD"),
120 : : G_("decrement of read-only reference %qD"),
121 : : TREE_OPERAND (arg, 0));
122 : 142 : else if (is_stub_object (arg))
123 : : {
124 : 11 : gcc_assert (errstring == lv_assign);
125 : 11 : error_at (loc, "assignment to read-only type %qT", TREE_TYPE (arg));
126 : : }
127 : : else
128 : 131 : readonly_error (loc, arg, errstring);
129 : 155 : }
130 : :
131 : : /* If TYPE has abstract virtual functions, issue an error about trying
132 : : to create an object of that type. DECL is the object declared, or
133 : : NULL_TREE if the declaration is unavailable, in which case USE specifies
134 : : the kind of invalid use. Returns 1 if an error occurred; zero if
135 : : all was well. */
136 : :
137 : : static int
138 : 330747994 : abstract_virtuals_error (tree decl, tree type, abstract_class_use use,
139 : : tsubst_flags_t complain)
140 : : {
141 : 330747994 : vec<tree, va_gc> *pure;
142 : :
143 : 330747994 : if (TREE_CODE (type) == ARRAY_TYPE)
144 : : {
145 : 821144 : decl = NULL_TREE;
146 : 821144 : use = ACU_ARRAY;
147 : 821144 : type = strip_array_types (type);
148 : : }
149 : :
150 : : /* This function applies only to classes. Any other entity can never
151 : : be abstract. */
152 : 330747994 : if (!CLASS_TYPE_P (type))
153 : : return 0;
154 : 51677050 : type = TYPE_MAIN_VARIANT (type);
155 : :
156 : : #if 0
157 : : /* Instantiation here seems to be required by the standard,
158 : : but breaks e.g. boost::bind. FIXME! */
159 : : /* In SFINAE, non-N3276 context, force instantiation. */
160 : : if (!(complain & (tf_error|tf_decltype)))
161 : : complete_type (type);
162 : : #endif
163 : :
164 : 51677050 : if (!TYPE_SIZE (type))
165 : : /* TYPE is being defined, and during that time
166 : : CLASSTYPE_PURE_VIRTUALS holds the inline friends. */
167 : : return 0;
168 : :
169 : 51676955 : pure = CLASSTYPE_PURE_VIRTUALS (type);
170 : 51676955 : if (!pure)
171 : : return 0;
172 : :
173 : 126 : if (!(complain & tf_error))
174 : : return 1;
175 : :
176 : 102 : auto_diagnostic_group d;
177 : 102 : if (decl)
178 : : {
179 : 51 : if (VAR_P (decl))
180 : 9 : error ("cannot declare variable %q+D to be of abstract "
181 : : "type %qT", decl, type);
182 : 42 : else if (TREE_CODE (decl) == PARM_DECL)
183 : : {
184 : 27 : if (DECL_NAME (decl))
185 : 24 : error ("cannot declare parameter %q+D to be of abstract type %qT",
186 : : decl, type);
187 : : else
188 : 3 : error ("cannot declare parameter to be of abstract type %qT",
189 : : type);
190 : : }
191 : 15 : else if (TREE_CODE (decl) == FIELD_DECL)
192 : 12 : error ("cannot declare field %q+D to be of abstract type %qT",
193 : : decl, type);
194 : 3 : else if (TREE_CODE (decl) == FUNCTION_DECL
195 : 3 : && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
196 : 0 : error ("invalid abstract return type for member function %q+#D", decl);
197 : 3 : else if (TREE_CODE (decl) == FUNCTION_DECL)
198 : 3 : error ("invalid abstract return type for function %q+#D", decl);
199 : 0 : else if (identifier_p (decl))
200 : : /* Here we do not have location information. */
201 : 0 : error ("invalid abstract type %qT for %qE", type, decl);
202 : : else
203 : 0 : error ("invalid abstract type for %q+D", decl);
204 : : }
205 : 51 : else switch (use)
206 : : {
207 : 9 : case ACU_ARRAY:
208 : 9 : error ("creating array of %qT, which is an abstract class type", type);
209 : 9 : break;
210 : 9 : case ACU_CAST:
211 : 9 : error ("invalid cast to abstract class type %qT", type);
212 : 9 : break;
213 : 0 : case ACU_NEW:
214 : 0 : error ("invalid new-expression of abstract class type %qT", type);
215 : 0 : break;
216 : 0 : case ACU_RETURN:
217 : 0 : error ("invalid abstract return type %qT", type);
218 : 0 : break;
219 : 0 : case ACU_PARM:
220 : 0 : error ("invalid abstract parameter type %qT", type);
221 : 0 : break;
222 : 6 : case ACU_THROW:
223 : 6 : error ("expression of abstract class type %qT cannot "
224 : : "be used in throw-expression", type);
225 : 6 : break;
226 : 3 : case ACU_CATCH:
227 : 3 : error ("cannot declare %<catch%> parameter to be of abstract "
228 : : "class type %qT", type);
229 : 3 : break;
230 : 24 : default:
231 : 24 : error ("cannot allocate an object of abstract type %qT", type);
232 : : }
233 : :
234 : : /* Only go through this once. */
235 : 102 : if (pure->length ())
236 : : {
237 : 42 : unsigned ix;
238 : 42 : tree fn;
239 : :
240 : 42 : inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
241 : : " because the following virtual functions are pure within %qT:",
242 : : type);
243 : :
244 : 126 : FOR_EACH_VEC_ELT (*pure, ix, fn)
245 : 84 : if (! DECL_CLONED_FUNCTION_P (fn)
246 : 42 : || DECL_COMPLETE_DESTRUCTOR_P (fn))
247 : 42 : inform (DECL_SOURCE_LOCATION (fn), " %#qD", fn);
248 : :
249 : : /* Now truncate the vector. This leaves it non-null, so we know
250 : : there are pure virtuals, but empty so we don't list them out
251 : : again. */
252 : 42 : pure->truncate (0);
253 : : }
254 : :
255 : 102 : return 1;
256 : 102 : }
257 : :
258 : : int
259 : 286411671 : abstract_virtuals_error (tree decl, tree type,
260 : : tsubst_flags_t complain /* = tf_warning_or_error */)
261 : : {
262 : 286411671 : return abstract_virtuals_error (decl, type, ACU_UNKNOWN, complain);
263 : : }
264 : :
265 : : int
266 : 44336323 : abstract_virtuals_error (abstract_class_use use, tree type,
267 : : tsubst_flags_t complain /* = tf_warning_or_error */)
268 : : {
269 : 44336323 : return abstract_virtuals_error (NULL_TREE, type, use, complain);
270 : : }
271 : :
272 : :
273 : : /* Print an inform about the declaration of the incomplete type TYPE. */
274 : :
275 : : void
276 : 928 : cxx_incomplete_type_inform (const_tree type)
277 : : {
278 : 928 : if (!TYPE_MAIN_DECL (type))
279 : : return;
280 : :
281 : 838 : location_t loc = DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type));
282 : 838 : tree ptype = strip_top_quals (CONST_CAST_TREE (type));
283 : :
284 : : /* When defining a template, current_class_type will be the pattern on
285 : : the template definition, while non-self-reference usages of this
286 : : template will be an instantiation; we should pull out the pattern to
287 : : compare against. And for partial specs we should use the loc of the
288 : : partial spec rather than the primary template. */
289 : 838 : tree ttype = NULL_TREE;
290 : 838 : tree tinfo = TYPE_TEMPLATE_INFO (ptype);
291 : 838 : if (tinfo)
292 : : {
293 : 197 : tree tmpl = TI_TEMPLATE (tinfo);
294 : 197 : if (PRIMARY_TEMPLATE_P (tmpl) && TI_PARTIAL_INFO (tinfo))
295 : : {
296 : 37 : tree partial = TI_TEMPLATE (TI_PARTIAL_INFO (tinfo));
297 : 37 : loc = DECL_SOURCE_LOCATION (partial);
298 : 37 : ttype = TREE_TYPE (partial);
299 : : }
300 : : else
301 : 160 : ttype = TREE_TYPE (tmpl);
302 : : }
303 : :
304 : 838 : if (current_class_type
305 : 275 : && TYPE_BEING_DEFINED (current_class_type)
306 : 1063 : && (same_type_p (ptype, current_class_type)
307 : 132 : || (ttype && same_type_p (ttype, current_class_type))))
308 : 101 : inform (loc, "definition of %q#T is not complete until "
309 : : "the closing brace", ptype);
310 : : else
311 : : {
312 : 737 : if (!tinfo)
313 : 599 : inform (loc, "forward declaration of %q#T", ptype);
314 : : else
315 : 138 : inform (loc, "declaration of %q#T", ptype);
316 : :
317 : : /* If there's a similar-looking complete type attached
318 : : to a different module, point at that as a suggestion. */
319 : 737 : if (modules_p () && TYPE_NAMESPACE_SCOPE_P (ptype))
320 : : {
321 : 24 : tree result = lookup_qualified_name (CP_TYPE_CONTEXT (ptype),
322 : 24 : TYPE_IDENTIFIER (ptype),
323 : : LOOK_want::TYPE);
324 : 24 : if (TREE_CODE (result) == TREE_LIST)
325 : 72 : for (; result; result = TREE_CHAIN (result))
326 : : {
327 : 48 : tree cand = TREE_VALUE (result);
328 : :
329 : : /* Typedefs are not likely intended to correspond. */
330 : 48 : if (is_typedef_decl (STRIP_TEMPLATE (cand))
331 : 42 : || DECL_ALIAS_TEMPLATE_P (cand))
332 : 6 : continue;
333 : :
334 : : /* Only look at templates if type was a template. */
335 : 42 : if ((tinfo != nullptr) != (TREE_CODE (cand) == TEMPLATE_DECL))
336 : 3 : continue;
337 : :
338 : : /* If we're looking for a template specialisation,
339 : : only consider matching specialisations. */
340 : 39 : if (tinfo)
341 : : {
342 : 27 : tree t = lookup_template_class (cand, TI_ARGS (tinfo),
343 : : NULL_TREE, NULL_TREE,
344 : : tf_none);
345 : 27 : if (t == error_mark_node
346 : 27 : || !CLASS_TYPE_P (t)
347 : 54 : || TYPE_BEING_DEFINED (t))
348 : 0 : continue;
349 : :
350 : 27 : if (CLASSTYPE_TEMPLATE_INSTANTIATION (t))
351 : : {
352 : : /* An uninstantiated template: check if there is a
353 : : pattern that could be used. We don't want to
354 : : call instantiate_class_template as that could
355 : : cause further errors; this is just a hint. */
356 : 21 : tree part = most_specialized_partial_spec (t, tf_none);
357 : 27 : cand = (part ? TI_TEMPLATE (part)
358 : 15 : : CLASSTYPE_TI_TEMPLATE (t));
359 : : }
360 : : else
361 : 6 : cand = TYPE_NAME (t);
362 : : }
363 : :
364 : 39 : if (!COMPLETE_TYPE_P (TREE_TYPE (cand)))
365 : 24 : continue;
366 : :
367 : 15 : inform (DECL_SOURCE_LOCATION (cand),
368 : : "%q#T has a definition but does not correspond with "
369 : : "%q#T because it is attached to a different module",
370 : 15 : TREE_TYPE (cand), ptype);
371 : : }
372 : : }
373 : : }
374 : : }
375 : :
376 : : /* Print an error message for invalid use of an incomplete type.
377 : : VALUE is the expression that was used (or 0 if that isn't known)
378 : : and TYPE is the type that was invalid. DIAG_KIND indicates the
379 : : type of diagnostic (see diagnostics/kinds.def). */
380 : :
381 : : bool
382 : 932 : cxx_incomplete_type_diagnostic (location_t loc, const_tree value,
383 : : const_tree type,
384 : : enum diagnostics::kind diag_kind)
385 : : {
386 : 932 : bool is_decl = false, complained = false;
387 : :
388 : : /* Avoid duplicate error message. */
389 : 932 : if (TREE_CODE (type) == ERROR_MARK)
390 : : return false;
391 : :
392 : 932 : auto_diagnostic_group d;
393 : 932 : if (value)
394 : : {
395 : 406 : STRIP_ANY_LOCATION_WRAPPER (value);
396 : :
397 : 406 : if (VAR_P (value)
398 : 361 : || TREE_CODE (value) == PARM_DECL
399 : 316 : || TREE_CODE (value) == FIELD_DECL)
400 : : {
401 : 117 : complained = emit_diagnostic (diag_kind, DECL_SOURCE_LOCATION (value), 0,
402 : : "%qD has incomplete type", value);
403 : 117 : is_decl = true;
404 : : }
405 : : }
406 : 932 : retry:
407 : : /* We must print an error message. Be clever about what it says. */
408 : :
409 : 938 : switch (TREE_CODE (type))
410 : : {
411 : 675 : case RECORD_TYPE:
412 : 675 : case UNION_TYPE:
413 : 675 : case ENUMERAL_TYPE:
414 : 675 : if (!is_decl)
415 : 579 : complained = emit_diagnostic (diag_kind, loc, 0,
416 : : "invalid use of incomplete type %q#T",
417 : : type);
418 : 675 : if (complained)
419 : 672 : cxx_incomplete_type_inform (type);
420 : : break;
421 : :
422 : 10 : case VOID_TYPE:
423 : 10 : complained = emit_diagnostic (diag_kind, loc, 0,
424 : : "invalid use of %qT", type);
425 : 10 : break;
426 : :
427 : 60 : case ARRAY_TYPE:
428 : 60 : if (TYPE_DOMAIN (type))
429 : : {
430 : 6 : type = TREE_TYPE (type);
431 : 6 : goto retry;
432 : : }
433 : 54 : complained = emit_diagnostic (diag_kind, loc, 0,
434 : : "invalid use of array with unspecified bounds");
435 : 54 : break;
436 : :
437 : 72 : case OFFSET_TYPE:
438 : 72 : bad_member:
439 : 72 : {
440 : 72 : tree member = TREE_OPERAND (value, 1);
441 : 72 : if (is_overloaded_fn (member) && !flag_ms_extensions)
442 : : {
443 : 69 : gcc_rich_location richloc (loc);
444 : : /* If "member" has no arguments (other than "this"), then
445 : : add a fix-it hint. */
446 : 69 : member = MAYBE_BASELINK_FUNCTIONS (member);
447 : 69 : if (TREE_CODE (member) == FUNCTION_DECL
448 : 57 : && DECL_OBJECT_MEMBER_FUNCTION_P (member)
449 : 126 : && type_num_arguments (TREE_TYPE (member)) == 1)
450 : 54 : richloc.add_fixit_insert_after ("()");
451 : 69 : complained = emit_diagnostic (diag_kind, &richloc, 0,
452 : : "invalid use of member function %qD "
453 : : "(did you forget the %<()%> ?)", member);
454 : 69 : }
455 : : else
456 : 3 : complained = emit_diagnostic (diag_kind, loc, 0,
457 : : "invalid use of member %qD "
458 : : "(did you forget the %<&%> ?)", member);
459 : : }
460 : : break;
461 : :
462 : 28 : case TEMPLATE_TYPE_PARM:
463 : 28 : if (is_auto (type))
464 : : {
465 : 16 : if (CLASS_PLACEHOLDER_TEMPLATE (type))
466 : 3 : complained = emit_diagnostic (diag_kind, loc, 0,
467 : : "invalid use of placeholder %qT", type);
468 : : else
469 : 13 : complained = emit_diagnostic (diag_kind, loc, 0,
470 : : "invalid use of %qT", type);
471 : : }
472 : : else
473 : 12 : complained = emit_diagnostic (diag_kind, loc, 0,
474 : : "invalid use of template type parameter %qT", type);
475 : : break;
476 : :
477 : 3 : case BOUND_TEMPLATE_TEMPLATE_PARM:
478 : 3 : complained = emit_diagnostic (diag_kind, loc, 0,
479 : : "invalid use of template template parameter %qT",
480 : 3 : TYPE_NAME (type));
481 : 3 : break;
482 : :
483 : 0 : case TYPE_PACK_EXPANSION:
484 : 0 : complained = emit_diagnostic (diag_kind, loc, 0,
485 : : "invalid use of pack expansion %qT", type);
486 : 0 : break;
487 : :
488 : 18 : case TYPENAME_TYPE:
489 : 18 : case DECLTYPE_TYPE:
490 : 18 : complained = emit_diagnostic (diag_kind, loc, 0,
491 : : "invalid use of dependent type %qT", type);
492 : 18 : break;
493 : :
494 : 144 : case LANG_TYPE:
495 : 144 : if (type == init_list_type_node)
496 : : {
497 : 3 : complained = emit_diagnostic (diag_kind, loc, 0,
498 : : "invalid use of brace-enclosed initializer list");
499 : 3 : break;
500 : : }
501 : 141 : gcc_assert (type == unknown_type_node);
502 : 141 : if (value && TREE_CODE (value) == COMPONENT_REF)
503 : 72 : goto bad_member;
504 : 69 : else if (value && TREE_CODE (value) == ADDR_EXPR)
505 : 27 : complained = emit_diagnostic (diag_kind, loc, 0,
506 : : "address of overloaded function with no contextual "
507 : : "type information");
508 : 42 : else if (value && TREE_CODE (value) == OVERLOAD)
509 : 42 : complained = emit_diagnostic (diag_kind, loc, 0,
510 : : "overloaded function with no contextual type information");
511 : : else
512 : 0 : complained = emit_diagnostic (diag_kind, loc, 0,
513 : : "insufficient contextual information to determine type");
514 : : break;
515 : :
516 : 0 : default:
517 : 0 : gcc_unreachable ();
518 : : }
519 : :
520 : 932 : return complained;
521 : 932 : }
522 : :
523 : : /* Print an error message for invalid use of an incomplete type.
524 : : VALUE is the expression that was used (or 0 if that isn't known)
525 : : and TYPE is the type that was invalid. */
526 : :
527 : : void
528 : 57 : cxx_incomplete_type_error (location_t loc, const_tree value, const_tree type)
529 : : {
530 : 57 : cxx_incomplete_type_diagnostic (loc, value, type, diagnostics::kind::error);
531 : 57 : }
532 : :
533 : :
534 : : /* We've just initialized subobject SUB; also insert a TARGET_EXPR with an
535 : : EH-only cleanup for SUB. Because of EH region nesting issues, we need to
536 : : make the cleanup conditional on a flag that we will clear once the object is
537 : : fully initialized, so push a new flag onto FLAGS. */
538 : :
539 : : static void
540 : 371491 : maybe_push_temp_cleanup (tree sub, vec<tree,va_gc> **flags)
541 : : {
542 : 371491 : if (!flag_exceptions)
543 : : return;
544 : 740276 : if (tree cleanup
545 : 370138 : = cxx_maybe_build_cleanup (sub, tf_warning_or_error))
546 : : {
547 : 226 : tree tx = get_internal_target_expr (boolean_true_node);
548 : 226 : tree flag = TARGET_EXPR_SLOT (tx);
549 : 226 : TARGET_EXPR_CLEANUP (tx) = build3 (COND_EXPR, void_type_node,
550 : : flag, cleanup, void_node);
551 : 226 : add_stmt (tx);
552 : 226 : vec_safe_push (*flags, flag);
553 : : }
554 : : }
555 : :
556 : : /* F is something added to a cleanup flags vec by maybe_push_temp_cleanup or
557 : : build_vec_init. Return the code to disable the cleanup it controls. */
558 : :
559 : : tree
560 : 730 : build_disable_temp_cleanup (tree f)
561 : : {
562 : 730 : tree d = f;
563 : 730 : tree i = boolean_false_node;
564 : 730 : if (TREE_CODE (f) == TREE_LIST)
565 : : {
566 : : /* To disable a build_vec_init cleanup, set
567 : : iterator = maxindex. */
568 : 504 : d = TREE_PURPOSE (f);
569 : 504 : i = TREE_VALUE (f);
570 : 504 : ggc_free (f);
571 : : }
572 : 730 : return build2 (MODIFY_EXPR, TREE_TYPE (d), d, i);
573 : : }
574 : :
575 : : /* The recursive part of split_nonconstant_init. DEST is an lvalue
576 : : expression to which INIT should be assigned. INIT is a CONSTRUCTOR.
577 : : Return true if the whole of the value was initialized by the
578 : : generated statements. */
579 : :
580 : : static bool
581 : 283771 : split_nonconstant_init_1 (tree dest, tree init, bool last,
582 : : vec<tree,va_gc> **flags)
583 : : {
584 : 283771 : unsigned HOST_WIDE_INT idx, tidx = HOST_WIDE_INT_M1U;
585 : 283771 : tree field_index, value;
586 : 283771 : tree type = TREE_TYPE (dest);
587 : 283771 : tree inner_type = NULL;
588 : 283771 : bool array_type_p = false;
589 : 283771 : bool complete_p = true;
590 : 283771 : HOST_WIDE_INT num_split_elts = 0;
591 : 283771 : tree last_split_elt = NULL_TREE;
592 : :
593 : 283771 : switch (TREE_CODE (type))
594 : : {
595 : 6416 : case ARRAY_TYPE:
596 : 6416 : inner_type = TREE_TYPE (type);
597 : 6416 : array_type_p = true;
598 : 6416 : if ((TREE_SIDE_EFFECTS (init)
599 : 590 : && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
600 : 6792 : || vla_type_p (type))
601 : : {
602 : 281 : if (!TYPE_DOMAIN (type)
603 : 3 : && TREE_CODE (init) == CONSTRUCTOR
604 : 284 : && CONSTRUCTOR_NELTS (init))
605 : : {
606 : : /* Flexible array. */
607 : 3 : cp_complete_array_type (&type, init, /*default*/true);
608 : 3 : dest = build1 (VIEW_CONVERT_EXPR, type, dest);
609 : : }
610 : :
611 : : /* For an array, we only need/want a single cleanup region rather
612 : : than one per element. build_vec_init will handle it. */
613 : 281 : tree code = build_vec_init (dest, NULL_TREE, init, false, 1,
614 : : tf_warning_or_error, flags);
615 : 281 : add_stmt (code);
616 : 281 : return true;
617 : : }
618 : : /* FALLTHRU */
619 : :
620 : 282429 : case RECORD_TYPE:
621 : 282429 : case UNION_TYPE:
622 : 282429 : case QUAL_UNION_TYPE:
623 : 927936 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), idx,
624 : : field_index, value)
625 : : {
626 : : /* The current implementation of this algorithm assumes that
627 : : the field was set for all the elements. This is usually done
628 : : by process_init_constructor. */
629 : 645507 : gcc_assert (field_index);
630 : :
631 : 645507 : if (!array_type_p)
632 : 635898 : inner_type = TREE_TYPE (field_index);
633 : :
634 : 645507 : tree sub;
635 : 645507 : if (array_type_p)
636 : 9609 : sub = build4 (ARRAY_REF, inner_type, dest, field_index,
637 : : NULL_TREE, NULL_TREE);
638 : : else
639 : 635898 : sub = build3 (COMPONENT_REF, inner_type, dest, field_index,
640 : : NULL_TREE);
641 : :
642 : 1933751 : bool elt_last = last && idx == CONSTRUCTOR_NELTS (init) - 1;
643 : :
644 : : /* We need to see sub-array TARGET_EXPR before cp_fold_r so we can
645 : : handle cleanup flags properly. */
646 : 645507 : gcc_checking_assert (!target_expr_needs_replace (value));
647 : :
648 : 645507 : if (TREE_CODE (value) == CONSTRUCTOR)
649 : : {
650 : 893 : if (!split_nonconstant_init_1 (sub, value, elt_last, flags)
651 : : /* For flexible array member with initializer we
652 : : can't remove the initializer, because only the
653 : : initializer determines how many elements the
654 : : flexible array member has. */
655 : 893 : || (!array_type_p
656 : 264 : && TREE_CODE (inner_type) == ARRAY_TYPE
657 : 162 : && TYPE_DOMAIN (inner_type) == NULL
658 : 49 : && TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
659 : 49 : && COMPLETE_TYPE_P (TREE_TYPE (value))
660 : 49 : && !integer_zerop (TYPE_SIZE (TREE_TYPE (value)))
661 : 49 : && elt_last
662 : 49 : && TYPE_HAS_TRIVIAL_DESTRUCTOR
663 : : (strip_array_types (inner_type))))
664 : : complete_p = false;
665 : : else
666 : : {
667 : : /* Mark element for removal. */
668 : 258 : last_split_elt = field_index;
669 : 258 : CONSTRUCTOR_ELT (init, idx)->index = NULL_TREE;
670 : 258 : if (idx < tidx)
671 : : tidx = idx;
672 : 258 : num_split_elts++;
673 : : }
674 : : }
675 : 644614 : else if (tree vi = get_vec_init_expr (value))
676 : : {
677 : 31 : add_stmt (expand_vec_init_expr (sub, vi, tf_warning_or_error,
678 : : flags));
679 : :
680 : : /* Mark element for removal. */
681 : 31 : last_split_elt = field_index;
682 : 31 : CONSTRUCTOR_ELT (init, idx)->index = NULL_TREE;
683 : 31 : if (idx < tidx)
684 : : tidx = idx;
685 : 31 : num_split_elts++;
686 : : }
687 : 644583 : else if (!initializer_constant_valid_p (value, inner_type))
688 : : {
689 : 536837 : tree code;
690 : :
691 : : /* Push cleanups for any preceding members with constant
692 : : initialization. */
693 : 536837 : if (CLASS_TYPE_P (type))
694 : 528287 : for (tree prev = (last_split_elt ?
695 : 263219 : DECL_CHAIN (last_split_elt)
696 : 528287 : : TYPE_FIELDS (type));
697 : 1488 : ; prev = DECL_CHAIN (prev))
698 : : {
699 : 529775 : prev = next_aggregate_field (prev);
700 : 529775 : if (prev == field_index)
701 : : break;
702 : 1488 : tree ptype = TREE_TYPE (prev);
703 : 1488 : if (TYPE_P (ptype) && type_build_dtor_call (ptype))
704 : : {
705 : 53 : tree pcref = build3 (COMPONENT_REF, ptype, dest, prev,
706 : : NULL_TREE);
707 : 53 : maybe_push_temp_cleanup (pcref, flags);
708 : : }
709 : 1488 : }
710 : :
711 : : /* Mark element for removal. */
712 : 536837 : CONSTRUCTOR_ELT (init, idx)->index = NULL_TREE;
713 : 536837 : if (idx < tidx)
714 : : tidx = idx;
715 : :
716 : 536837 : if (TREE_CODE (field_index) == RANGE_EXPR)
717 : : {
718 : : /* Use build_vec_init to initialize a range. */
719 : 0 : tree low = TREE_OPERAND (field_index, 0);
720 : 0 : tree hi = TREE_OPERAND (field_index, 1);
721 : 0 : sub = build4 (ARRAY_REF, inner_type, dest, low,
722 : : NULL_TREE, NULL_TREE);
723 : 0 : sub = cp_build_addr_expr (sub, tf_warning_or_error);
724 : 0 : tree max = size_binop (MINUS_EXPR, hi, low);
725 : 0 : code = build_vec_init (sub, max, value, false, 0,
726 : : tf_warning_or_error);
727 : 0 : add_stmt (code);
728 : 0 : if (tree_fits_shwi_p (max))
729 : 0 : num_split_elts += tree_to_shwi (max);
730 : : }
731 : : else
732 : : {
733 : : /* We may need to add a copy constructor call if
734 : : the field has [[no_unique_address]]. */
735 : 536837 : if (unsafe_return_slot_p (sub))
736 : : {
737 : : /* But not if the initializer is an implicit ctor call
738 : : we just built in digest_init. */
739 : 1202 : if (TREE_CODE (value) == TARGET_EXPR
740 : 1202 : && TARGET_EXPR_LIST_INIT_P (value)
741 : 1916 : && make_safe_copy_elision (sub, value))
742 : 714 : goto build_init;
743 : :
744 : 488 : if (TREE_CODE (value) == TARGET_EXPR)
745 : : /* We have to add this constructor, so we will not
746 : : elide. */
747 : 488 : TARGET_EXPR_ELIDING_P (value) = false;
748 : :
749 : 488 : tree name = (DECL_FIELD_IS_BASE (field_index)
750 : 488 : ? base_ctor_identifier
751 : 488 : : complete_ctor_identifier);
752 : 488 : releasing_vec args = make_tree_vector_single (value);
753 : 488 : code = build_special_member_call
754 : 488 : (sub, name, &args, inner_type,
755 : : LOOKUP_NORMAL, tf_warning_or_error);
756 : 488 : }
757 : : else
758 : : {
759 : 535635 : build_init:
760 : 536349 : code = cp_build_init_expr (sub, value);
761 : : }
762 : 536837 : code = build_stmt (input_location, EXPR_STMT, code);
763 : 536837 : add_stmt (code);
764 : 536837 : if (!elt_last)
765 : 371438 : maybe_push_temp_cleanup (sub, flags);
766 : : }
767 : :
768 : 536837 : last_split_elt = field_index;
769 : 536837 : num_split_elts++;
770 : : }
771 : : }
772 : 282429 : if (num_split_elts == 1)
773 : 137881 : CONSTRUCTOR_ELTS (init)->ordered_remove (tidx);
774 : 144548 : else if (num_split_elts > 1)
775 : : {
776 : : /* Perform the delayed ordered removal of non-constant elements
777 : : we split out. */
778 : 533031 : for (idx = tidx; idx < CONSTRUCTOR_NELTS (init); ++idx)
779 : 399563 : if (CONSTRUCTOR_ELT (init, idx)->index == NULL_TREE)
780 : : ;
781 : : else
782 : : {
783 : 318 : *CONSTRUCTOR_ELT (init, tidx) = *CONSTRUCTOR_ELT (init, idx);
784 : 318 : ++tidx;
785 : : }
786 : 133468 : vec_safe_truncate (CONSTRUCTOR_ELTS (init), tidx);
787 : : }
788 : : break;
789 : :
790 : 1061 : case VECTOR_TYPE:
791 : 1061 : if (!initializer_constant_valid_p (init, type))
792 : : {
793 : 1056 : tree code;
794 : 1056 : tree cons = copy_node (init);
795 : 1056 : CONSTRUCTOR_ELTS (init) = NULL;
796 : 1056 : code = build2 (MODIFY_EXPR, type, dest, cons);
797 : 1056 : code = build_stmt (input_location, EXPR_STMT, code);
798 : 1056 : add_stmt (code);
799 : 1056 : num_split_elts += CONSTRUCTOR_NELTS (init);
800 : : }
801 : : break;
802 : :
803 : 0 : default:
804 : 0 : gcc_unreachable ();
805 : : }
806 : :
807 : : /* The rest of the initializer is now a constant. */
808 : 283490 : TREE_CONSTANT (init) = 1;
809 : 283490 : TREE_SIDE_EFFECTS (init) = 0;
810 : :
811 : : /* We didn't split out anything. */
812 : 283490 : if (num_split_elts == 0)
813 : : return false;
814 : :
815 : 271349 : return complete_p && complete_ctor_at_level_p (TREE_TYPE (init),
816 : : num_split_elts, inner_type);
817 : : }
818 : :
819 : : /* A subroutine of store_init_value. Splits non-constant static
820 : : initializer INIT into a constant part and generates code to
821 : : perform the non-constant part of the initialization to DEST.
822 : : Returns the code for the runtime init. */
823 : :
824 : : tree
825 : 13395289 : split_nonconstant_init (tree dest, tree init)
826 : : {
827 : 13395289 : tree code;
828 : :
829 : 13395289 : if (TREE_CODE (init) == TARGET_EXPR)
830 : 292297 : init = TARGET_EXPR_INITIAL (init);
831 : 13395289 : if (TREE_CODE (init) == CONSTRUCTOR)
832 : : {
833 : : /* Subobject initializers are not full-expressions. */
834 : 282878 : auto fe = (make_temp_override
835 : 282878 : (current_stmt_tree ()->stmts_are_full_exprs_p, 0));
836 : :
837 : 282878 : init = cp_fully_fold_init (init);
838 : 282878 : code = push_stmt_list ();
839 : :
840 : : /* If the complete object is an array, build_vec_init's cleanup is
841 : : enough. Otherwise, collect flags for disabling subobject
842 : : cleanups once the complete object is fully constructed. */
843 : 282878 : vec<tree, va_gc> *flags = nullptr;
844 : 282878 : if (TREE_CODE (TREE_TYPE (dest)) != ARRAY_TYPE)
845 : 276773 : flags = make_tree_vector ();
846 : :
847 : 282878 : if (split_nonconstant_init_1 (dest, init, true, &flags))
848 : 165030 : init = NULL_TREE;
849 : :
850 : 837261 : for (tree f : flags)
851 : 459 : finish_expr_stmt (build_disable_temp_cleanup (f));
852 : 282878 : release_tree_vector (flags);
853 : :
854 : 282878 : code = pop_stmt_list (code);
855 : 282878 : if (VAR_P (dest) && !is_local_temp (dest))
856 : : {
857 : 280994 : DECL_INITIAL (dest) = init;
858 : 280994 : TREE_READONLY (dest) = 0;
859 : : }
860 : 1884 : else if (init)
861 : : {
862 : 367 : tree ie = cp_build_init_expr (dest, init);
863 : 367 : code = add_stmt_to_compound (ie, code);
864 : : }
865 : 282878 : }
866 : 13112411 : else if (TREE_CODE (init) == STRING_CST
867 : 13112411 : && array_of_runtime_bound_p (TREE_TYPE (dest)))
868 : 25 : code = build_vec_init (dest, NULL_TREE, init, /*value-init*/false,
869 : : /*from array*/1, tf_warning_or_error);
870 : : else
871 : 13112386 : code = cp_build_init_expr (dest, init);
872 : :
873 : 13395289 : return code;
874 : : }
875 : :
876 : : /* T is the initializer of a constexpr variable. Set CONSTRUCTOR_MUTABLE_POISON
877 : : for any CONSTRUCTOR within T that contains (directly or indirectly) a mutable
878 : : member, thereby poisoning it so it can't be copied to another a constexpr
879 : : variable or read during constexpr evaluation. */
880 : :
881 : : static void
882 : 22047120 : poison_mutable_constructors (tree t)
883 : : {
884 : 22047120 : if (TREE_CODE (t) != CONSTRUCTOR)
885 : : return;
886 : :
887 : 1176311 : if (cp_has_mutable_p (TREE_TYPE (t)))
888 : : {
889 : 159 : CONSTRUCTOR_MUTABLE_POISON (t) = true;
890 : :
891 : 159 : if (vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (t))
892 : 341 : for (const constructor_elt &ce : *elts)
893 : 188 : poison_mutable_constructors (ce.value);
894 : : }
895 : : }
896 : :
897 : : /* Perform appropriate conversions on the initial value of a variable,
898 : : store it in the declaration DECL,
899 : : and print any error messages that are appropriate.
900 : : If the init is invalid, store an ERROR_MARK.
901 : :
902 : : C++: Note that INIT might be a TREE_LIST, which would mean that it is
903 : : a base class initializer for some aggregate type, hopefully compatible
904 : : with DECL. If INIT is a single element, and DECL is an aggregate
905 : : type, we silently convert INIT into a TREE_LIST, allowing a constructor
906 : : to be called.
907 : :
908 : : If INIT is a TREE_LIST and there is no constructor, turn INIT
909 : : into a CONSTRUCTOR and use standard initialization techniques.
910 : : Perhaps a warning should be generated?
911 : :
912 : : Returns code to be executed if initialization could not be performed
913 : : for static variable. In that case, caller must emit the code. */
914 : :
915 : : tree
916 : 38063758 : store_init_value (tree decl, tree init, vec<tree, va_gc>** cleanups, int flags)
917 : : {
918 : 38063758 : tree value, type;
919 : :
920 : : /* If variable's type was invalidly declared, just ignore it. */
921 : :
922 : 38063758 : type = TREE_TYPE (decl);
923 : 38063758 : if (TREE_CODE (type) == ERROR_MARK)
924 : : return NULL_TREE;
925 : :
926 : 2544051 : if (MAYBE_CLASS_TYPE_P (type))
927 : : {
928 : 2512361 : if (TREE_CODE (init) == TREE_LIST)
929 : : {
930 : 0 : error ("constructor syntax used, but no constructor declared "
931 : : "for type %qT", type);
932 : 0 : init = build_constructor_from_list (init_list_type_node, nreverse (init));
933 : : }
934 : : }
935 : :
936 : : /* End of special C++ code. */
937 : :
938 : 38063758 : if (flags & LOOKUP_ALREADY_DIGESTED)
939 : : value = init;
940 : : else
941 : : {
942 : 36318625 : if (TREE_STATIC (decl))
943 : 18196663 : flags |= LOOKUP_ALLOW_FLEXARRAY_INIT;
944 : : /* Digest the specified initializer into an expression. */
945 : 36318625 : value = digest_init_flags (type, init, flags, tf_warning_or_error);
946 : : }
947 : :
948 : : /* Look for braced array initializers for character arrays and
949 : : recursively convert them into STRING_CSTs. */
950 : 38063758 : value = braced_lists_to_strings (type, value);
951 : :
952 : 38063758 : current_ref_temp_count = 0;
953 : 38063758 : value = extend_ref_init_temps (decl, value, cleanups);
954 : :
955 : : /* In C++11 constant expression is a semantic, not syntactic, property.
956 : : In C++98, make sure that what we thought was a constant expression at
957 : : template definition time is still constant and otherwise perform this
958 : : as optimization, e.g. to fold SIZEOF_EXPRs in the initializer. */
959 : 38063758 : if (decl_maybe_constant_var_p (decl) || TREE_STATIC (decl))
960 : : {
961 : 22049629 : bool const_init;
962 : 22049629 : tree oldval = value;
963 : 22049629 : if (DECL_DECLARED_CONSTEXPR_P (decl)
964 : 5469897 : || DECL_DECLARED_CONSTINIT_P (decl)
965 : 27519136 : || (DECL_IN_AGGR_P (decl)
966 : 1121584 : && DECL_INITIALIZED_IN_CLASS_P (decl)))
967 : : {
968 : 17701576 : value = fold_non_dependent_expr (value, tf_warning_or_error,
969 : : /*manifestly_const_eval=*/true,
970 : : decl);
971 : 17698879 : if (value == error_mark_node)
972 : : ;
973 : : /* Diagnose a non-constant initializer for constexpr variable or
974 : : non-inline in-class-initialized static data member. */
975 : 17698670 : else if (!is_constant_expression (value))
976 : : {
977 : : /* Maybe we want to give this message for constexpr variables as
978 : : well, but that will mean a lot of testsuite adjustment. */
979 : 258 : if (DECL_DECLARED_CONSTINIT_P (decl))
980 : 88 : error_at (location_of (decl),
981 : : "%<constinit%> variable %qD does not have a "
982 : : "constant initializer", decl);
983 : 258 : require_constant_expression (value);
984 : 258 : value = error_mark_node;
985 : : }
986 : : else
987 : : {
988 : 17698412 : value = maybe_constant_init (value, decl, true);
989 : :
990 : : /* In a template we might not have done the necessary
991 : : transformations to make value actually constant,
992 : : e.g. extend_ref_init_temps. */
993 : 17698412 : if (!processing_template_decl
994 : 17698412 : && !TREE_CONSTANT (value))
995 : : {
996 : 1470 : if (DECL_DECLARED_CONSTINIT_P (decl))
997 : 41 : error_at (location_of (decl),
998 : : "%<constinit%> variable %qD does not have a "
999 : : "constant initializer", decl);
1000 : 1470 : value = cxx_constant_init (value, decl);
1001 : : }
1002 : : }
1003 : : }
1004 : : else
1005 : 4348053 : value = fold_non_dependent_init (value, tf_warning_or_error,
1006 : : /*manifestly_const_eval=*/true, decl);
1007 : 22046932 : poison_mutable_constructors (value);
1008 : 22046932 : const_init = (reduced_constant_expression_p (value)
1009 : 22046932 : || error_operand_p (value));
1010 : 22046932 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = const_init;
1011 : : /* FIXME setting TREE_CONSTANT on refs breaks the back end. */
1012 : 22046932 : if (!TYPE_REF_P (type))
1013 : 24480517 : TREE_CONSTANT (decl) = const_init && decl_maybe_constant_var_p (decl);
1014 : 22046932 : if (!const_init)
1015 : 18138848 : value = oldval;
1016 : : }
1017 : : /* Don't fold initializers of automatic variables in constexpr functions,
1018 : : that might fold away something that needs to be diagnosed at constexpr
1019 : : evaluation time. */
1020 : 38061061 : if (!current_function_decl
1021 : 19784771 : || !DECL_DECLARED_CONSTEXPR_P (current_function_decl)
1022 : 41229320 : || TREE_STATIC (decl))
1023 : 34892888 : value = cp_fully_fold_init (value);
1024 : :
1025 : : /* Handle aggregate NSDMI in non-constant initializers, too. */
1026 : 38061061 : value = replace_placeholders (value, decl);
1027 : :
1028 : : /* A COMPOUND_LITERAL_P CONSTRUCTOR is the syntactic form; by the time we get
1029 : : here it should have been digested into an actual value for the type. */
1030 : 38061061 : gcc_checking_assert (TREE_CODE (value) != CONSTRUCTOR
1031 : : || processing_template_decl
1032 : : || VECTOR_TYPE_P (type)
1033 : : || !TREE_HAS_CONSTRUCTOR (value));
1034 : :
1035 : : /* If the initializer is not a constant, fill in DECL_INITIAL with
1036 : : the bits that are constant, and then return an expression that
1037 : : will perform the dynamic initialization. */
1038 : 38061061 : if (value != error_mark_node
1039 : 38056390 : && !processing_template_decl
1040 : 74329979 : && (TREE_SIDE_EFFECTS (value)
1041 : 30289991 : || vla_type_p (type)
1042 : 30289899 : || ! reduced_constant_expression_p (value)))
1043 : 13384054 : return split_nonconstant_init (decl, value);
1044 : :
1045 : : /* DECL may change value; purge caches. */
1046 : 24677007 : clear_cv_and_fold_caches ();
1047 : :
1048 : : /* If the value is a constant, just put it in DECL_INITIAL. If DECL
1049 : : is an automatic variable, the middle end will turn this into a
1050 : : dynamic initialization later. */
1051 : 24677007 : DECL_INITIAL (decl) = value;
1052 : 24677007 : return NULL_TREE;
1053 : : }
1054 : :
1055 : :
1056 : : /* Give diagnostic about narrowing conversions within { }, or as part of
1057 : : a converted constant expression. If CONST_ONLY, only check
1058 : : constants. */
1059 : :
1060 : : bool
1061 : 20732504 : check_narrowing (tree type, tree init, tsubst_flags_t complain,
1062 : : bool const_only/*= false*/)
1063 : : {
1064 : 20732504 : tree ftype = unlowered_expr_type (init);
1065 : 20732504 : bool ok = true;
1066 : 20732504 : REAL_VALUE_TYPE d;
1067 : :
1068 : 20689831 : if (((!warn_narrowing || !(complain & tf_warning))
1069 : 2820289 : && cxx_dialect == cxx98)
1070 : 20690736 : || !ARITHMETIC_TYPE_P (type)
1071 : : /* Don't emit bogus warnings with e.g. value-dependent trees. */
1072 : 41283574 : || instantiation_dependent_expression_p (init))
1073 : 260778 : return ok;
1074 : :
1075 : 14 : if (BRACE_ENCLOSED_INITIALIZER_P (init)
1076 : 20471726 : && TREE_CODE (type) == COMPLEX_TYPE)
1077 : : {
1078 : 0 : tree elttype = TREE_TYPE (type);
1079 : 0 : if (CONSTRUCTOR_NELTS (init) > 0)
1080 : 0 : ok &= check_narrowing (elttype, CONSTRUCTOR_ELT (init, 0)->value,
1081 : : complain);
1082 : 0 : if (CONSTRUCTOR_NELTS (init) > 1)
1083 : 0 : ok &= check_narrowing (elttype, CONSTRUCTOR_ELT (init, 1)->value,
1084 : : complain);
1085 : 0 : return ok;
1086 : : }
1087 : :
1088 : : /* Even non-dependent expressions can still have template
1089 : : codes like CAST_EXPR, so use *_non_dependent_expr to cope. */
1090 : 20471726 : init = fold_non_dependent_expr (init, complain, /*manifest*/true);
1091 : 20471726 : if (init == error_mark_node)
1092 : : return ok;
1093 : :
1094 : : /* If we were asked to only check constants, return early. */
1095 : 20471723 : if (const_only && !TREE_CONSTANT (init))
1096 : : return ok;
1097 : :
1098 : 20470230 : if (CP_INTEGRAL_TYPE_P (type)
1099 : 20456898 : && SCALAR_FLOAT_TYPE_P (ftype))
1100 : : ok = false;
1101 : 20470008 : else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype)
1102 : 20408031 : && CP_INTEGRAL_TYPE_P (type))
1103 : : {
1104 : 20402000 : if (TREE_CODE (ftype) == ENUMERAL_TYPE)
1105 : : /* Check for narrowing based on the values of the enumeration. */
1106 : 315847 : ftype = ENUM_UNDERLYING_TYPE (ftype);
1107 : : /* Undo convert_bitfield_to_declared_type (STRIP_NOPS isn't enough). */
1108 : 20402000 : tree op = init;
1109 : 20403273 : while (CONVERT_EXPR_P (op))
1110 : 1273 : op = TREE_OPERAND (op, 0);
1111 : : /* Core 2627 says that we shouldn't warn when "the source is a bit-field
1112 : : whose width w is less than that of its type (or, for an enumeration
1113 : : type, its underlying type) and the target type can represent all the
1114 : : values of a hypothetical extended integer type with width w and with
1115 : : the same signedness as the original type". */
1116 : 20402000 : if (is_bitfield_expr_with_lowered_type (op)
1117 : 20402000 : && TYPE_PRECISION (TREE_TYPE (op)) < TYPE_PRECISION (ftype))
1118 : 70 : ftype = TREE_TYPE (op);
1119 : 20402000 : if ((tree_int_cst_lt (TYPE_MAX_VALUE (type),
1120 : 20402000 : TYPE_MAX_VALUE (ftype))
1121 : 20138953 : || tree_int_cst_lt (TYPE_MIN_VALUE (ftype),
1122 : 20138953 : TYPE_MIN_VALUE (type)))
1123 : 39123112 : && (TREE_CODE (init) != INTEGER_CST
1124 : 18984041 : || !int_fits_type_p (init, type)))
1125 : : ok = false;
1126 : : }
1127 : : /* [dcl.init.list]#7.2: "from long double to double or float, or from
1128 : : double to float". */
1129 : 68008 : else if (SCALAR_FLOAT_TYPE_P (ftype)
1130 : 7301 : && SCALAR_FLOAT_TYPE_P (type))
1131 : : {
1132 : 14620 : if ((extended_float_type_p (ftype) || extended_float_type_p (type))
1133 : 7289 : ? /* "from a floating-point type T to another floating-point type
1134 : : whose floating-point conversion rank is neither greater than
1135 : : nor equal to that of T".
1136 : : So, it is ok if
1137 : : cp_compare_floating_point_conversion_ranks (ftype, type)
1138 : : returns -2 (type has greater conversion rank than ftype)
1139 : : or [-1..1] (type has equal conversion rank as ftype, possibly
1140 : : different subrank. Only do this if at least one of the
1141 : : types is extended floating-point type, otherwise keep doing
1142 : : what we did before (for the sake of non-standard
1143 : : backend types). */
1144 : 209 : cp_compare_floating_point_conversion_ranks (ftype, type) >= 2
1145 : 7080 : : ((same_type_p (ftype, long_double_type_node)
1146 : 125 : && (same_type_p (type, double_type_node)
1147 : 114 : || same_type_p (type, float_type_node)))
1148 : 7067 : || (same_type_p (ftype, double_type_node)
1149 : 6400 : && same_type_p (type, float_type_node))
1150 : 9080 : || (TYPE_PRECISION (type) < TYPE_PRECISION (ftype))))
1151 : : {
1152 : 5136 : if (TREE_CODE (init) == REAL_CST)
1153 : : {
1154 : : /* Issue 703: Loss of precision is OK as long as the value is
1155 : : within the representable range of the new type. */
1156 : 5060 : REAL_VALUE_TYPE r;
1157 : 5060 : d = TREE_REAL_CST (init);
1158 : 5060 : real_convert (&r, TYPE_MODE (type), &d);
1159 : 5060 : if (real_isinf (&r))
1160 : 0 : ok = false;
1161 : : }
1162 : : else
1163 : : ok = false;
1164 : : }
1165 : : }
1166 : 60719 : else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype)
1167 : 6031 : && SCALAR_FLOAT_TYPE_P (type))
1168 : : {
1169 : 5996 : ok = false;
1170 : 5996 : if (TREE_CODE (init) == INTEGER_CST)
1171 : : {
1172 : 5956 : d = real_value_from_int_cst (0, init);
1173 : 5956 : if (exact_real_truncate (TYPE_MODE (type), &d))
1174 : : ok = true;
1175 : : }
1176 : : }
1177 : 54723 : else if (TREE_CODE (type) == BOOLEAN_TYPE
1178 : 54723 : && (TYPE_PTR_P (ftype) || TYPE_PTRMEM_P (ftype)))
1179 : : /* C++20 P1957R2: converting from a pointer type or a pointer-to-member
1180 : : type to bool should be considered narrowing. This is a DR so is not
1181 : : limited to C++20 only. */
1182 : : ok = false;
1183 : :
1184 : 5965 : bool almost_ok = ok;
1185 : 5965 : if (!ok && !CONSTANT_CLASS_P (init) && (complain & tf_warning_or_error))
1186 : : {
1187 : 161 : tree folded = cp_fully_fold (init);
1188 : 161 : if (TREE_CONSTANT (folded) && check_narrowing (type, folded, tf_none))
1189 : : almost_ok = true;
1190 : : }
1191 : :
1192 : 905 : if (!ok)
1193 : : {
1194 : 905 : location_t loc = cp_expr_loc_or_input_loc (init);
1195 : 905 : if (cxx_dialect == cxx98)
1196 : : {
1197 : 1 : if (complain & tf_warning)
1198 : 1 : warning_at (loc, OPT_Wnarrowing, "narrowing conversion of %qE "
1199 : : "from %qH to %qI is ill-formed in C++11",
1200 : : init, ftype, type);
1201 : : ok = true;
1202 : : }
1203 : 904 : else if (!CONSTANT_CLASS_P (init))
1204 : : {
1205 : 292 : if (complain & tf_warning_or_error)
1206 : : {
1207 : 161 : auto_diagnostic_group d;
1208 : 4 : if ((!almost_ok || pedantic)
1209 : 304 : && pedwarn (loc, OPT_Wnarrowing,
1210 : : "narrowing conversion of %qE from %qH to %qI",
1211 : : init, ftype, type)
1212 : 306 : && almost_ok)
1213 : 2 : inform (loc, " the expression has a constant value but is not "
1214 : : "a C++ constant-expression");
1215 : 161 : ok = true;
1216 : 161 : }
1217 : : }
1218 : 612 : else if (complain & tf_error)
1219 : : {
1220 : 598 : int savederrorcount = errorcount;
1221 : 598 : permerror_opt (loc, OPT_Wnarrowing,
1222 : : "narrowing conversion of %qE from %qH to %qI",
1223 : : init, ftype, type);
1224 : 598 : if (errorcount == savederrorcount)
1225 : 20731876 : ok = true;
1226 : : }
1227 : : }
1228 : :
1229 : : return ok;
1230 : : }
1231 : :
1232 : : /* True iff TYPE is a C++20 "ordinary" character type. */
1233 : :
1234 : : bool
1235 : 40417 : ordinary_char_type_p (tree type)
1236 : : {
1237 : 40417 : type = TYPE_MAIN_VARIANT (type);
1238 : 40417 : return (type == char_type_node
1239 : 20267 : || type == signed_char_type_node
1240 : 60662 : || type == unsigned_char_type_node);
1241 : : }
1242 : :
1243 : : /* True iff the string literal INIT has a type suitable for initializing array
1244 : : TYPE. */
1245 : :
1246 : : bool
1247 : 574599 : array_string_literal_compatible_p (tree type, tree init)
1248 : : {
1249 : 574599 : tree to_char_type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
1250 : 574599 : tree from_char_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (init)));
1251 : :
1252 : 574599 : if (to_char_type == from_char_type)
1253 : : return true;
1254 : : /* The array element type does not match the initializing string
1255 : : literal element type; this is only allowed when both types are
1256 : : ordinary character type. There are no string literals of
1257 : : signed or unsigned char type in the language, but we can get
1258 : : them internally from converting braced-init-lists to
1259 : : STRING_CST. */
1260 : 20253 : if (ordinary_char_type_p (to_char_type)
1261 : 20253 : && ordinary_char_type_p (from_char_type))
1262 : : return true;
1263 : :
1264 : : /* P2513 (C++20/C++23): "an array of char or unsigned char may
1265 : : be initialized by a UTF-8 string literal, or by such a string
1266 : : literal enclosed in braces." */
1267 : 137 : if (from_char_type == char8_type_node
1268 : 36 : && (to_char_type == char_type_node
1269 : 23 : || to_char_type == unsigned_char_type_node))
1270 : : return true;
1271 : :
1272 : : return false;
1273 : : }
1274 : :
1275 : : /* Process the initializer INIT for a variable of type TYPE, emitting
1276 : : diagnostics for invalid initializers and converting the initializer as
1277 : : appropriate.
1278 : :
1279 : : For aggregate types, it assumes that reshape_init has already run, thus the
1280 : : initializer will have the right shape (brace elision has been undone).
1281 : :
1282 : : NESTED is non-zero iff we are being called for an element of a CONSTRUCTOR,
1283 : : 2 iff the element of a CONSTRUCTOR is inside another CONSTRUCTOR. */
1284 : :
1285 : : static tree
1286 : 65351490 : digest_init_r (tree type, tree init, int nested, int flags,
1287 : : tsubst_flags_t complain)
1288 : : {
1289 : 65351490 : enum tree_code code = TREE_CODE (type);
1290 : :
1291 : 65351490 : if (error_operand_p (init))
1292 : 1962 : return error_mark_node;
1293 : :
1294 : 65349528 : gcc_assert (init);
1295 : :
1296 : : /* We must strip the outermost array type when completing the type,
1297 : : because the its bounds might be incomplete at the moment. */
1298 : 66231655 : if (!complete_type_or_maybe_complain (code == ARRAY_TYPE
1299 : 882127 : ? TREE_TYPE (type) : type, NULL_TREE,
1300 : : complain))
1301 : 27 : return error_mark_node;
1302 : :
1303 : 65349501 : location_t loc = cp_expr_loc_or_input_loc (init);
1304 : :
1305 : 65349501 : tree stripped_init = init;
1306 : :
1307 : 8726253 : if (BRACE_ENCLOSED_INITIALIZER_P (init)
1308 : 74072323 : && CONSTRUCTOR_IS_PAREN_INIT (init))
1309 : 1398 : flags |= LOOKUP_AGGREGATE_PAREN_INIT;
1310 : :
1311 : : /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue
1312 : : (g++.old-deja/g++.law/casts2.C). */
1313 : 65349501 : if (TREE_CODE (init) == NON_LVALUE_EXPR)
1314 : 15357036 : stripped_init = TREE_OPERAND (init, 0);
1315 : :
1316 : 65349501 : stripped_init = tree_strip_any_location_wrapper (stripped_init);
1317 : :
1318 : : /* Initialization of an array of chars from a string constant. The initializer
1319 : : can be optionally enclosed in braces, but reshape_init has already removed
1320 : : them if they were present. */
1321 : 65349501 : if (code == ARRAY_TYPE)
1322 : : {
1323 : 998263 : if (nested && !TYPE_DOMAIN (type))
1324 : : /* C++ flexible array members have a null domain. */
1325 : : {
1326 : 458 : if (flags & LOOKUP_ALLOW_FLEXARRAY_INIT)
1327 : 407 : pedwarn (loc, OPT_Wpedantic,
1328 : : "initialization of a flexible array member");
1329 : : else
1330 : : {
1331 : 51 : if (complain & tf_error)
1332 : 51 : error_at (loc, "non-static initialization of"
1333 : : " a flexible array member");
1334 : 51 : return error_mark_node;
1335 : : }
1336 : : }
1337 : :
1338 : 882076 : tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
1339 : 882076 : if (char_type_p (typ1)
1340 : 882076 : && TREE_CODE (stripped_init) == STRING_CST)
1341 : : {
1342 : 574554 : if (!array_string_literal_compatible_p (type, init))
1343 : : {
1344 : 111 : if (complain & tf_error)
1345 : 111 : error_at (loc, "cannot initialize array of %qT from "
1346 : : "a string literal with type array of %qT",
1347 : : typ1,
1348 : 111 : TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (init))));
1349 : 111 : return error_mark_node;
1350 : : }
1351 : :
1352 : 574816 : if (nested == 2 && !TYPE_DOMAIN (type))
1353 : : {
1354 : 24 : if (complain & tf_error)
1355 : 24 : error_at (loc, "initialization of flexible array member "
1356 : : "in a nested context");
1357 : 24 : return error_mark_node;
1358 : : }
1359 : :
1360 : 574419 : if (type != TREE_TYPE (init)
1361 : 574419 : && !variably_modified_type_p (type, NULL_TREE))
1362 : : {
1363 : 22272 : init = copy_node (init);
1364 : 22272 : TREE_TYPE (init) = type;
1365 : : /* If we have a location wrapper, then also copy the wrapped
1366 : : node, and update the copy's type. */
1367 : 22272 : if (location_wrapper_p (init))
1368 : : {
1369 : 21401 : stripped_init = copy_node (stripped_init);
1370 : 21401 : TREE_OPERAND (init, 0) = stripped_init;
1371 : 21401 : TREE_TYPE (stripped_init) = type;
1372 : : }
1373 : : }
1374 : 574419 : if (TYPE_DOMAIN (type) && TREE_CONSTANT (TYPE_SIZE (type)))
1375 : : {
1376 : : /* Not a flexible array member. */
1377 : 574349 : int size = TREE_INT_CST_LOW (TYPE_SIZE (type));
1378 : 574349 : size = (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
1379 : : /* In C it is ok to subtract 1 from the length of the string
1380 : : because it's ok to ignore the terminating null char that is
1381 : : counted in the length of the constant, but in C++ this would
1382 : : be invalid. */
1383 : 574349 : if (size < TREE_STRING_LENGTH (stripped_init))
1384 : : {
1385 : 159 : permerror (loc, "initializer-string for %qT is too long",
1386 : : type);
1387 : :
1388 : 159 : init = build_string (size,
1389 : 159 : TREE_STRING_POINTER (stripped_init));
1390 : 159 : TREE_TYPE (init) = type;
1391 : : }
1392 : : }
1393 : 574419 : return init;
1394 : : }
1395 : : }
1396 : :
1397 : : /* Handle scalar types (including conversions) and references. */
1398 : 1133 : if ((code != COMPLEX_TYPE || BRACE_ENCLOSED_INITIALIZER_P (stripped_init))
1399 : 64774941 : && (SCALAR_TYPE_P (type) || code == REFERENCE_TYPE))
1400 : : {
1401 : : /* Narrowing is OK when initializing an aggregate from
1402 : : a parenthesized list. */
1403 : 56210824 : if (nested && !(flags & LOOKUP_AGGREGATE_PAREN_INIT))
1404 : 21046907 : flags |= LOOKUP_NO_NARROWING;
1405 : 56210824 : if (TREE_CODE (init) == RAW_DATA_CST && !TYPE_UNSIGNED (type))
1406 : : {
1407 : 48 : tree ret = init;
1408 : 48 : if ((flags & LOOKUP_NO_NARROWING) || warn_conversion)
1409 : 11022 : for (unsigned int i = 0;
1410 : 11070 : i < (unsigned) RAW_DATA_LENGTH (init); ++i)
1411 : 11022 : if (RAW_DATA_SCHAR_ELT (init, i) < 0)
1412 : : {
1413 : 60 : if ((flags & LOOKUP_NO_NARROWING))
1414 : : {
1415 : 60 : tree elt
1416 : 60 : = build_int_cst (integer_type_node,
1417 : 60 : RAW_DATA_UCHAR_ELT (init, i));
1418 : 60 : if (!check_narrowing (type, elt, complain, false))
1419 : : {
1420 : 30 : if (!(complain & tf_warning_or_error))
1421 : 0 : ret = error_mark_node;
1422 : 30 : continue;
1423 : : }
1424 : : }
1425 : 30 : if (warn_conversion)
1426 : 60 : warning (OPT_Wconversion,
1427 : : "conversion from %qT to %qT changes value from "
1428 : : "%qd to %qd",
1429 : : integer_type_node, type,
1430 : 30 : RAW_DATA_UCHAR_ELT (init, i),
1431 : 30 : RAW_DATA_SCHAR_ELT (init, i));
1432 : : }
1433 : 48 : return ret;
1434 : : }
1435 : 56210776 : init = convert_for_initialization (0, type, init, flags,
1436 : : ICR_INIT, NULL_TREE, 0,
1437 : : complain);
1438 : :
1439 : 56210776 : return init;
1440 : : }
1441 : :
1442 : : /* Come here only for aggregates: records, arrays, unions, complex numbers
1443 : : and vectors. */
1444 : 8564072 : gcc_assert (code == ARRAY_TYPE
1445 : : || VECTOR_TYPE_P (type)
1446 : : || code == RECORD_TYPE
1447 : : || code == UNION_TYPE
1448 : : || code == OPAQUE_TYPE
1449 : : || code == COMPLEX_TYPE);
1450 : :
1451 : : /* "If T is a class type and the initializer list has a single
1452 : : element of type cv U, where U is T or a class derived from T,
1453 : : the object is initialized from that element." */
1454 : 8564072 : if (cxx_dialect >= cxx11
1455 : 8537389 : && BRACE_ENCLOSED_INITIALIZER_P (stripped_init)
1456 : 8301677 : && !CONSTRUCTOR_IS_DESIGNATED_INIT (stripped_init)
1457 : 8026063 : && CONSTRUCTOR_NELTS (stripped_init) == 1
1458 : 8980537 : && ((CLASS_TYPE_P (type) && !CLASSTYPE_NON_AGGREGATE (type))
1459 : 51097 : || VECTOR_TYPE_P (type)))
1460 : : {
1461 : 365655 : tree elt = CONSTRUCTOR_ELT (stripped_init, 0)->value;
1462 : 365655 : if (reference_related_p (type, TREE_TYPE (elt)))
1463 : : {
1464 : : /* In C++17, aggregates can have bases, thus participate in
1465 : : aggregate initialization. In the following case:
1466 : :
1467 : : struct B { int c; };
1468 : : struct D : B { };
1469 : : D d{{D{{42}}}};
1470 : :
1471 : : there's an extra set of braces, so the D temporary initializes
1472 : : the first element of d, which is the B base subobject. The base
1473 : : of type B is copy-initialized from the D temporary, causing
1474 : : object slicing. */
1475 : 30 : tree field = next_aggregate_field (TYPE_FIELDS (type));
1476 : 60 : if (field && DECL_FIELD_IS_BASE (field))
1477 : : {
1478 : 30 : if (warning_at (loc, 0, "initializing a base class of type %qT "
1479 : 30 : "results in object slicing", TREE_TYPE (field)))
1480 : 30 : inform (loc, "remove %<{ }%> around initializer");
1481 : : }
1482 : 0 : else if (flag_checking)
1483 : : /* We should have fixed this in reshape_init. */
1484 : 0 : gcc_unreachable ();
1485 : : }
1486 : : }
1487 : :
1488 : 8564072 : if (SIMPLE_TARGET_EXPR_P (stripped_init))
1489 : 8326 : stripped_init = TARGET_EXPR_INITIAL (stripped_init);
1490 : :
1491 : 8321386 : if (BRACE_ENCLOSED_INITIALIZER_P (stripped_init)
1492 : 16881009 : && !TYPE_NON_AGGREGATE_CLASS (type))
1493 : 8012195 : return process_init_constructor (type, stripped_init, nested, flags,
1494 : 8012195 : complain);
1495 : : else
1496 : : {
1497 : 551877 : if (COMPOUND_LITERAL_P (stripped_init) && code == ARRAY_TYPE)
1498 : : {
1499 : 0 : if (complain & tf_error)
1500 : 0 : error_at (loc, "cannot initialize aggregate of type %qT with "
1501 : : "a compound literal", type);
1502 : :
1503 : 0 : return error_mark_node;
1504 : : }
1505 : :
1506 : 551877 : if (code == ARRAY_TYPE
1507 : 551877 : && !BRACE_ENCLOSED_INITIALIZER_P (stripped_init))
1508 : : {
1509 : : /* Allow the result of build_array_copy and of
1510 : : build_value_init_noctor. */
1511 : 94 : if ((TREE_CODE (stripped_init) == VEC_INIT_EXPR
1512 : 75 : || TREE_CODE (stripped_init) == CONSTRUCTOR)
1513 : 144 : && (same_type_ignoring_top_level_qualifiers_p
1514 : 50 : (type, TREE_TYPE (init))))
1515 : : return init;
1516 : :
1517 : 53 : if (complain & tf_error)
1518 : 53 : error_at (loc, "array must be initialized with a brace-enclosed"
1519 : : " initializer");
1520 : 53 : return error_mark_node;
1521 : : }
1522 : :
1523 : 551783 : return convert_for_initialization (NULL_TREE, type, init,
1524 : : flags,
1525 : : ICR_INIT, NULL_TREE, 0,
1526 : 551783 : complain);
1527 : : }
1528 : : }
1529 : :
1530 : : tree
1531 : 1182837 : digest_init (tree type, tree init, tsubst_flags_t complain)
1532 : : {
1533 : 1182837 : return digest_init_r (type, init, 0, LOOKUP_IMPLICIT, complain);
1534 : : }
1535 : :
1536 : : tree
1537 : 40669422 : digest_init_flags (tree type, tree init, int flags, tsubst_flags_t complain)
1538 : : {
1539 : 40669422 : return digest_init_r (type, init, 0, flags, complain);
1540 : : }
1541 : :
1542 : : /* Callback to replace PLACEHOLDER_EXPRs in a TARGET_EXPR (which isn't used
1543 : : in the context of guaranteed copy elision). */
1544 : :
1545 : : static tree
1546 : 2501482 : replace_placeholders_for_class_temp_r (tree *tp, int *, void *)
1547 : : {
1548 : 2501482 : tree t = *tp;
1549 : :
1550 : : /* We're looking for a TARGET_EXPR nested in the whole expression. */
1551 : 2501482 : if (TREE_CODE (t) == TARGET_EXPR
1552 : : /* That serves as temporary materialization, not an initializer. */
1553 : 2501482 : && !TARGET_EXPR_ELIDING_P (t))
1554 : : {
1555 : 764 : tree init = TARGET_EXPR_INITIAL (t);
1556 : 767 : while (TREE_CODE (init) == COMPOUND_EXPR)
1557 : 3 : init = TREE_OPERAND (init, 1);
1558 : 764 : if (TREE_CODE (init) == CONSTRUCTOR
1559 : 764 : && CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init))
1560 : : {
1561 : 129 : tree obj = TARGET_EXPR_SLOT (t);
1562 : 129 : replace_placeholders (init, obj);
1563 : : /* We should have dealt with all PLACEHOLDER_EXPRs. */
1564 : 129 : CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init) = false;
1565 : 129 : gcc_checking_assert (!find_placeholders (init));
1566 : : }
1567 : : }
1568 : :
1569 : 2501482 : return NULL_TREE;
1570 : : }
1571 : :
1572 : : /* Process the initializer INIT for an NSDMI DECL (a FIELD_DECL). */
1573 : : tree
1574 : 509112 : digest_nsdmi_init (tree decl, tree init, tsubst_flags_t complain)
1575 : : {
1576 : 509112 : gcc_assert (TREE_CODE (decl) == FIELD_DECL);
1577 : :
1578 : 509112 : tree type = TREE_TYPE (decl);
1579 : 509112 : if (DECL_BIT_FIELD_TYPE (decl))
1580 : 3284 : type = DECL_BIT_FIELD_TYPE (decl);
1581 : 509112 : int flags = LOOKUP_IMPLICIT;
1582 : 509112 : if (DIRECT_LIST_INIT_P (init))
1583 : : {
1584 : 63354 : flags = LOOKUP_NORMAL;
1585 : 63354 : complain |= tf_no_cleanup;
1586 : : }
1587 : 184853 : if (BRACE_ENCLOSED_INITIALIZER_P (init)
1588 : 693965 : && CP_AGGREGATE_TYPE_P (type))
1589 : 139073 : init = reshape_init (type, init, complain);
1590 : 509112 : init = digest_init_flags (type, init, flags, complain);
1591 : 509112 : set_target_expr_eliding (init);
1592 : :
1593 : : /* We may have temporary materialization in a NSDMI, if the initializer
1594 : : has something like A{} in it. Digesting the {} could have introduced
1595 : : a PLACEHOLDER_EXPR referring to A. Now that we've got a TARGET_EXPR,
1596 : : we have an object we can refer to. The reason we bother doing this
1597 : : here is for code like
1598 : :
1599 : : struct A {
1600 : : int x;
1601 : : int y = x;
1602 : : };
1603 : :
1604 : : struct B {
1605 : : int x = 0;
1606 : : int y = A{x}.y; // #1
1607 : : };
1608 : :
1609 : : where in #1 we don't want to end up with two PLACEHOLDER_EXPRs for
1610 : : different types on the same level in a {} when lookup_placeholder
1611 : : wouldn't find a named object for the PLACEHOLDER_EXPR for A. Note,
1612 : : temporary materialization does not occur when initializing an object
1613 : : from a prvalue of the same type, therefore we must not replace the
1614 : : placeholder with a temporary object so that it can be elided. */
1615 : 509112 : cp_walk_tree_without_duplicates (&init, replace_placeholders_for_class_temp_r,
1616 : : nullptr);
1617 : :
1618 : 509112 : return init;
1619 : : }
1620 : :
1621 : : /* Set of flags used within process_init_constructor to describe the
1622 : : initializers. */
1623 : : #define PICFLAG_ERRONEOUS 1
1624 : : #define PICFLAG_NOT_ALL_CONSTANT 2
1625 : : #define PICFLAG_NOT_ALL_SIMPLE 4
1626 : : #define PICFLAG_SIDE_EFFECTS 8
1627 : : #define PICFLAG_VEC_INIT 16
1628 : :
1629 : : /* Given an initializer INIT, return the flag (PICFLAG_*) which better
1630 : : describe it. */
1631 : :
1632 : : static int
1633 : 24096057 : picflag_from_initializer (tree init)
1634 : : {
1635 : 24096057 : if (init == error_mark_node)
1636 : : return PICFLAG_ERRONEOUS;
1637 : 24095632 : else if (!TREE_CONSTANT (init))
1638 : : {
1639 : 1292459 : if (TREE_SIDE_EFFECTS (init))
1640 : : return PICFLAG_SIDE_EFFECTS;
1641 : : else
1642 : 1202052 : return PICFLAG_NOT_ALL_CONSTANT;
1643 : : }
1644 : 22803173 : else if (!initializer_constant_valid_p (init, TREE_TYPE (init)))
1645 : 13017 : return PICFLAG_NOT_ALL_SIMPLE;
1646 : : return 0;
1647 : : }
1648 : :
1649 : : /* Adjust INIT for going into a CONSTRUCTOR. */
1650 : :
1651 : : static tree
1652 : 23499231 : massage_init_elt (tree type, tree init, int nested, int flags,
1653 : : tsubst_flags_t complain)
1654 : : {
1655 : 23499231 : int new_flags = LOOKUP_IMPLICIT;
1656 : 23499231 : if (flags & LOOKUP_ALLOW_FLEXARRAY_INIT)
1657 : 19731187 : new_flags |= LOOKUP_ALLOW_FLEXARRAY_INIT;
1658 : 23499231 : if (flags & LOOKUP_AGGREGATE_PAREN_INIT)
1659 : 2176 : new_flags |= LOOKUP_AGGREGATE_PAREN_INIT;
1660 : 42177888 : init = digest_init_r (type, init, nested ? 2 : 1, new_flags, complain);
1661 : : /* When we defer constant folding within a statement, we may want to
1662 : : defer this folding as well. Don't call this on CONSTRUCTORs in
1663 : : a template because their elements have already been folded, and
1664 : : we must avoid folding the result of get_nsdmi. */
1665 : 23499231 : if (!(processing_template_decl && TREE_CODE (init) == CONSTRUCTOR))
1666 : : {
1667 : 23499074 : tree t = fold_non_dependent_init (init, complain);
1668 : 23499074 : if (TREE_CONSTANT (t))
1669 : 22194342 : init = t;
1670 : 23499074 : set_target_expr_eliding (init);
1671 : : }
1672 : 23499231 : return init;
1673 : : }
1674 : :
1675 : : /* Subroutine of process_init_constructor, which will process an initializer
1676 : : INIT for an array or vector of type TYPE. Returns the flags (PICFLAG_*)
1677 : : which describe the initializers. */
1678 : :
1679 : : static int
1680 : 348775 : process_init_constructor_array (tree type, tree init, int nested, int flags,
1681 : : tsubst_flags_t complain)
1682 : : {
1683 : 348775 : unsigned HOST_WIDE_INT i, j, len = 0;
1684 : 348775 : int picflags = 0;
1685 : 348775 : bool unbounded = false;
1686 : 348775 : constructor_elt *ce;
1687 : 348775 : vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (init);
1688 : :
1689 : 348775 : gcc_assert (TREE_CODE (type) == ARRAY_TYPE
1690 : : || VECTOR_TYPE_P (type));
1691 : :
1692 : 348775 : if (TREE_CODE (type) == ARRAY_TYPE)
1693 : : {
1694 : : /* C++ flexible array members have a null domain. */
1695 : 307428 : tree domain = TYPE_DOMAIN (type);
1696 : 307428 : if (domain && TREE_CONSTANT (TYPE_MAX_VALUE (domain)))
1697 : 613960 : len = wi::ext (wi::to_offset (TYPE_MAX_VALUE (domain))
1698 : 613960 : - wi::to_offset (TYPE_MIN_VALUE (domain)) + 1,
1699 : 306980 : TYPE_PRECISION (TREE_TYPE (domain)),
1700 : 613960 : TYPE_SIGN (TREE_TYPE (domain))).to_uhwi ();
1701 : : else
1702 : : unbounded = true; /* Take as many as there are. */
1703 : :
1704 : 307428 : if (nested == 2 && !domain && !vec_safe_is_empty (v))
1705 : : {
1706 : 69 : if (complain & tf_error)
1707 : 138 : error_at (cp_expr_loc_or_input_loc (init),
1708 : : "initialization of flexible array member "
1709 : : "in a nested context");
1710 : 69 : return PICFLAG_ERRONEOUS;
1711 : : }
1712 : : }
1713 : : else
1714 : : /* Vectors are like simple fixed-size arrays. */
1715 : 41347 : unbounded = !TYPE_VECTOR_SUBPARTS (type).is_constant (&len);
1716 : :
1717 : : /* There must not be more initializers than needed. */
1718 : 348706 : if (!unbounded && vec_safe_length (v) > len)
1719 : : {
1720 : 13 : if (complain & tf_error)
1721 : 13 : error ("too many initializers for %qT", type);
1722 : : else
1723 : : return PICFLAG_ERRONEOUS;
1724 : : }
1725 : :
1726 : 348706 : j = 0;
1727 : 16915018 : FOR_EACH_VEC_SAFE_ELT (v, i, ce)
1728 : : {
1729 : 16566312 : if (!ce->index)
1730 : 538 : ce->index = size_int (j);
1731 : 16565774 : else if (!check_array_designated_initializer (ce, j))
1732 : 0 : ce->index = error_mark_node;
1733 : 16566312 : gcc_assert (ce->value);
1734 : 16566312 : ce->value
1735 : 16566312 : = massage_init_elt (TREE_TYPE (type), ce->value, nested, flags,
1736 : : complain);
1737 : :
1738 : 16566312 : gcc_checking_assert
1739 : : (ce->value == error_mark_node
1740 : : || (same_type_ignoring_top_level_qualifiers_p
1741 : : (strip_array_types (TREE_TYPE (type)),
1742 : : strip_array_types (TREE_TYPE (ce->value)))));
1743 : :
1744 : 16566312 : picflags |= picflag_from_initializer (ce->value);
1745 : : /* Propagate CONSTRUCTOR_PLACEHOLDER_BOUNDARY to outer
1746 : : CONSTRUCTOR. */
1747 : 16566312 : if (TREE_CODE (ce->value) == CONSTRUCTOR
1748 : 16566312 : && CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ce->value))
1749 : : {
1750 : 17 : CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init) = 1;
1751 : 17 : CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ce->value) = 0;
1752 : : }
1753 : 16566312 : if (TREE_CODE (ce->value) == RAW_DATA_CST)
1754 : 276 : j += RAW_DATA_LENGTH (ce->value);
1755 : : else
1756 : 16566036 : ++j;
1757 : : }
1758 : :
1759 : : /* No more initializers. If the array is unbounded, we are done. Otherwise,
1760 : : we must add initializers ourselves. */
1761 : 348706 : if (!unbounded)
1762 : 348405 : for (; i < len; ++i)
1763 : : {
1764 : 23104 : tree next;
1765 : :
1766 : 23104 : if (type_build_ctor_call (TREE_TYPE (type)))
1767 : : {
1768 : : /* If this type needs constructors run for default-initialization,
1769 : : we can't rely on the back end to do it for us, so make the
1770 : : initialization explicit by list-initializing from T{}. */
1771 : 393 : next = build_constructor (init_list_type_node, NULL);
1772 : 393 : next = massage_init_elt (TREE_TYPE (type), next, nested, flags,
1773 : : complain);
1774 : 393 : if (initializer_zerop (next))
1775 : : /* The default zero-initialization is fine for us; don't
1776 : : add anything to the CONSTRUCTOR. */
1777 : : next = NULL_TREE;
1778 : : }
1779 : 22711 : else if (!zero_init_p (TREE_TYPE (type)))
1780 : 138 : next = build_zero_init (TREE_TYPE (type),
1781 : : /*nelts=*/NULL_TREE,
1782 : : /*static_storage_p=*/false);
1783 : : else
1784 : : /* The default zero-initialization is fine for us; don't
1785 : : add anything to the CONSTRUCTOR. */
1786 : : next = NULL_TREE;
1787 : :
1788 : 284 : if (next)
1789 : : {
1790 : 284 : if (next != error_mark_node
1791 : 284 : && (initializer_constant_valid_p (next, TREE_TYPE (next))
1792 : 272 : != null_pointer_node))
1793 : : {
1794 : : /* Use VEC_INIT_EXPR for non-constant initialization of
1795 : : trailing elements with no explicit initializers. */
1796 : 119 : picflags |= PICFLAG_VEC_INIT;
1797 : 119 : break;
1798 : : }
1799 : :
1800 : 165 : picflags |= picflag_from_initializer (next);
1801 : : /* Propagate CONSTRUCTOR_PLACEHOLDER_BOUNDARY to outer
1802 : : CONSTRUCTOR. */
1803 : 165 : if (TREE_CODE (next) == CONSTRUCTOR
1804 : 165 : && CONSTRUCTOR_PLACEHOLDER_BOUNDARY (next))
1805 : : {
1806 : 0 : CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init) = 1;
1807 : 0 : CONSTRUCTOR_PLACEHOLDER_BOUNDARY (next) = 0;
1808 : : }
1809 : 165 : if (len > i+1)
1810 : : {
1811 : 87 : tree range = build2 (RANGE_EXPR, size_type_node,
1812 : 87 : build_int_cst (size_type_node, i),
1813 : 87 : build_int_cst (size_type_node, len - 1));
1814 : 87 : CONSTRUCTOR_APPEND_ELT (v, range, next);
1815 : 87 : break;
1816 : : }
1817 : : else
1818 : 78 : CONSTRUCTOR_APPEND_ELT (v, size_int (i), next);
1819 : : }
1820 : : else
1821 : : /* Don't bother checking all the other elements. */
1822 : : break;
1823 : : }
1824 : :
1825 : 348706 : CONSTRUCTOR_ELTS (init) = v;
1826 : 348706 : return picflags;
1827 : : }
1828 : :
1829 : : /* Subroutine of process_init_constructor, which will process an initializer
1830 : : INIT for a class of type TYPE. Returns the flags (PICFLAG_*) which describe
1831 : : the initializers. */
1832 : :
1833 : : static int
1834 : 7542981 : process_init_constructor_record (tree type, tree init, int nested, int flags,
1835 : : tsubst_flags_t complain)
1836 : : {
1837 : 7542981 : vec<constructor_elt, va_gc> *v = NULL;
1838 : 7542981 : tree field;
1839 : 7542981 : int skipped = 0;
1840 : :
1841 : 7542981 : gcc_assert (TREE_CODE (type) == RECORD_TYPE);
1842 : 7542981 : gcc_assert (!CLASSTYPE_VBASECLASSES (type));
1843 : 7542981 : gcc_assert (!TYPE_BINFO (type)
1844 : : || cxx_dialect >= cxx17
1845 : : || !BINFO_N_BASE_BINFOS (TYPE_BINFO (type)));
1846 : 7542981 : gcc_assert (!TYPE_POLYMORPHIC_P (type));
1847 : :
1848 : 7542981 : restart:
1849 : 7586403 : int picflags = 0;
1850 : 7586403 : unsigned HOST_WIDE_INT idx = 0;
1851 : 7586403 : int designator_skip = -1;
1852 : : /* Generally, we will always have an index for each initializer (which is
1853 : : a FIELD_DECL, put by reshape_init), but compound literals don't go trough
1854 : : reshape_init. So we need to handle both cases. */
1855 : 72071488 : for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
1856 : : {
1857 : 64529079 : tree next;
1858 : :
1859 : 120719034 : if (TREE_CODE (field) != FIELD_DECL
1860 : 64529079 : || (DECL_ARTIFICIAL (field)
1861 : 524065 : && !(cxx_dialect >= cxx17 && DECL_FIELD_IS_BASE (field))))
1862 : 56189955 : continue;
1863 : :
1864 : 8339124 : if (DECL_UNNAMED_BIT_FIELD (field))
1865 : 113 : continue;
1866 : :
1867 : : /* If this is a bitfield, first convert to the declared type. */
1868 : 8339011 : tree fldtype = TREE_TYPE (field);
1869 : 8339011 : if (DECL_BIT_FIELD_TYPE (field))
1870 : 480245 : fldtype = DECL_BIT_FIELD_TYPE (field);
1871 : 8339011 : if (fldtype == error_mark_node)
1872 : : return PICFLAG_ERRONEOUS;
1873 : :
1874 : 8338996 : next = NULL_TREE;
1875 : 8338996 : if (idx < CONSTRUCTOR_NELTS (init))
1876 : : {
1877 : 6816806 : constructor_elt *ce = &(*CONSTRUCTOR_ELTS (init))[idx];
1878 : 6816806 : if (ce->index)
1879 : : {
1880 : : /* We can have either a FIELD_DECL or an IDENTIFIER_NODE. The
1881 : : latter case can happen in templates where lookup has to be
1882 : : deferred. */
1883 : 6815185 : gcc_assert (TREE_CODE (ce->index) == FIELD_DECL
1884 : : || identifier_p (ce->index));
1885 : 6815185 : if (ce->index == field || ce->index == DECL_NAME (field))
1886 : 6815074 : next = ce->value;
1887 : : else
1888 : : {
1889 : 111 : ce = NULL;
1890 : 111 : if (designator_skip == -1)
1891 : : designator_skip = 1;
1892 : : }
1893 : : }
1894 : : else
1895 : : {
1896 : 1621 : designator_skip = 0;
1897 : 1621 : next = ce->value;
1898 : : }
1899 : :
1900 : 6816695 : if (ce)
1901 : : {
1902 : 6816695 : gcc_assert (ce->value);
1903 : 6816695 : next = massage_init_elt (fldtype, next, nested, flags, complain);
1904 : 6816695 : ++idx;
1905 : : }
1906 : : }
1907 : 8338996 : if (next == error_mark_node)
1908 : : /* We skip initializers for empty bases/fields, so skipping an invalid
1909 : : one could make us accept invalid code. */
1910 : : return PICFLAG_ERRONEOUS;
1911 : 8338439 : else if (next)
1912 : : /* Already handled above. */;
1913 : 1522301 : else if (DECL_INITIAL (field))
1914 : : {
1915 : 121306 : if (skipped > 0)
1916 : : {
1917 : : /* We're using an NSDMI past a field with implicit
1918 : : zero-init. Go back and make it explicit. */
1919 : 43422 : skipped = -1;
1920 : 43422 : vec_safe_truncate (v, 0);
1921 : 43422 : goto restart;
1922 : : }
1923 : : /* C++14 aggregate NSDMI. */
1924 : 77884 : next = get_nsdmi (field, /*ctor*/false, complain);
1925 : 77884 : if (!CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init)
1926 : 77884 : && find_placeholders (next))
1927 : 959 : CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init) = 1;
1928 : : }
1929 : 1400995 : else if (type_build_ctor_call (fldtype))
1930 : : {
1931 : : /* If this type needs constructors run for
1932 : : default-initialization, we can't rely on the back end to do it
1933 : : for us, so build up TARGET_EXPRs. If the type in question is
1934 : : a class, just build one up; if it's an array, recurse. */
1935 : 5302 : next = build_constructor (init_list_type_node, NULL);
1936 : 5302 : next = massage_init_elt (fldtype, next, nested, flags, complain);
1937 : 5302 : if (TREE_CODE (next) == TARGET_EXPR
1938 : 5302 : && unsafe_copy_elision_p (field, next))
1939 : 0 : TARGET_EXPR_ELIDING_P (next) = false;
1940 : :
1941 : : /* Warn when some struct elements are implicitly initialized. */
1942 : 5302 : if ((complain & tf_warning)
1943 : 4965 : && !cp_unevaluated_operand
1944 : 10243 : && !EMPTY_CONSTRUCTOR_P (init))
1945 : 27 : warning (OPT_Wmissing_field_initializers,
1946 : : "missing initializer for member %qD", field);
1947 : : }
1948 : : else
1949 : : {
1950 : 1395693 : if (TYPE_REF_P (fldtype))
1951 : : {
1952 : 21 : if (complain & tf_error)
1953 : 21 : error ("member %qD is uninitialized reference", field);
1954 : : else
1955 : : return PICFLAG_ERRONEOUS;
1956 : : }
1957 : 1395672 : else if (CLASSTYPE_REF_FIELDS_NEED_INIT (fldtype))
1958 : : {
1959 : 1 : if (complain & tf_error)
1960 : 1 : error ("member %qD with uninitialized reference fields", field);
1961 : : else
1962 : : return PICFLAG_ERRONEOUS;
1963 : : }
1964 : : /* Do nothing for flexible array members since they need not have any
1965 : : elements. Don't worry about 'skipped' because a flexarray has to
1966 : : be the last field. */
1967 : 1395671 : else if (TREE_CODE (fldtype) == ARRAY_TYPE && !TYPE_DOMAIN (fldtype))
1968 : 103 : continue;
1969 : :
1970 : : /* Warn when some struct elements are implicitly initialized
1971 : : to zero. */
1972 : 1395590 : if ((complain & tf_warning)
1973 : 1372604 : && !cp_unevaluated_operand
1974 : 1372069 : && !EMPTY_CONSTRUCTOR_P (init)
1975 : 1396757 : && !is_really_empty_class (fldtype, /*ignore_vptr*/false))
1976 : 1090 : warning (OPT_Wmissing_field_initializers,
1977 : : "missing initializer for member %qD", field);
1978 : :
1979 : 1395590 : if (!zero_init_p (fldtype) || skipped < 0)
1980 : : {
1981 : 563463 : if (TYPE_REF_P (fldtype))
1982 : 3 : next = build_zero_cst (fldtype);
1983 : : else
1984 : 563460 : next = build_zero_init (fldtype, /*nelts=*/NULL_TREE,
1985 : : /*static_storage_p=*/false);
1986 : : }
1987 : : else
1988 : : {
1989 : : /* The default zero-initialization is fine for us; don't
1990 : : add anything to the CONSTRUCTOR. */
1991 : 832127 : skipped = 1;
1992 : 832127 : continue;
1993 : : }
1994 : : }
1995 : :
1996 : : /* We can't actually elide the temporary when initializing a
1997 : : potentially-overlapping field from a function that returns by
1998 : : value. */
1999 : 7462787 : if (TREE_CODE (next) == TARGET_EXPR
2000 : 7462787 : && unsafe_copy_elision_p (field, next))
2001 : 25 : TARGET_EXPR_ELIDING_P (next) = false;
2002 : :
2003 : 7462787 : if (is_empty_field (field)
2004 : 7462787 : && !TREE_SIDE_EFFECTS (next))
2005 : : /* Don't add trivial initialization of an empty base/field to the
2006 : : constructor, as they might not be ordered the way the back-end
2007 : : expects. */
2008 : 43737 : continue;
2009 : :
2010 : : /* If this is a bitfield, now convert to the lowered type. */
2011 : 7419050 : if (fldtype != TREE_TYPE (field))
2012 : 434168 : next = cp_convert_and_check (TREE_TYPE (field), next, complain);
2013 : 7419050 : picflags |= picflag_from_initializer (next);
2014 : : /* Propagate CONSTRUCTOR_PLACEHOLDER_BOUNDARY to outer CONSTRUCTOR. */
2015 : 7419050 : if (TREE_CODE (next) == CONSTRUCTOR
2016 : 7419050 : && CONSTRUCTOR_PLACEHOLDER_BOUNDARY (next))
2017 : : {
2018 : 66 : CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init) = 1;
2019 : 66 : CONSTRUCTOR_PLACEHOLDER_BOUNDARY (next) = 0;
2020 : : }
2021 : 71904135 : CONSTRUCTOR_APPEND_ELT (v, field, next);
2022 : : }
2023 : :
2024 : 7542409 : if (idx < CONSTRUCTOR_NELTS (init))
2025 : : {
2026 : 105 : if (complain & tf_error)
2027 : : {
2028 : 22 : constructor_elt *ce = &(*CONSTRUCTOR_ELTS (init))[idx];
2029 : : /* For better diagnostics, try to find out if it is really
2030 : : the case of too many initializers or if designators are
2031 : : in incorrect order. */
2032 : 22 : if (designator_skip == 1 && ce->index)
2033 : : {
2034 : 15 : gcc_assert (TREE_CODE (ce->index) == FIELD_DECL
2035 : : || identifier_p (ce->index));
2036 : 15 : for (field = TYPE_FIELDS (type);
2037 : 54 : field; field = DECL_CHAIN (field))
2038 : : {
2039 : 90 : if (TREE_CODE (field) != FIELD_DECL
2040 : 54 : || (DECL_ARTIFICIAL (field)
2041 : 0 : && !(cxx_dialect >= cxx17
2042 : 0 : && DECL_FIELD_IS_BASE (field))))
2043 : 36 : continue;
2044 : :
2045 : 18 : if (DECL_UNNAMED_BIT_FIELD (field))
2046 : 0 : continue;
2047 : :
2048 : 18 : if (ce->index == field || ce->index == DECL_NAME (field))
2049 : : break;
2050 : : }
2051 : : }
2052 : 22 : if (field)
2053 : 15 : error ("designator order for field %qD does not match declaration "
2054 : : "order in %qT", field, type);
2055 : : else
2056 : 7 : error ("too many initializers for %qT", type);
2057 : : }
2058 : : else
2059 : : return PICFLAG_ERRONEOUS;
2060 : : }
2061 : :
2062 : 7542326 : CONSTRUCTOR_ELTS (init) = v;
2063 : 7542326 : return picflags;
2064 : : }
2065 : :
2066 : : /* Subroutine of process_init_constructor, which will process a single
2067 : : initializer INIT for a union of type TYPE. Returns the flags (PICFLAG_*)
2068 : : which describe the initializer. */
2069 : :
2070 : : static int
2071 : 120439 : process_init_constructor_union (tree type, tree init, int nested, int flags,
2072 : : tsubst_flags_t complain)
2073 : : {
2074 : 120439 : constructor_elt *ce;
2075 : 120439 : int len;
2076 : :
2077 : : /* If the initializer was empty, use the union's NSDMI if it has one.
2078 : : Otherwise use default zero initialization. */
2079 : 120439 : if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
2080 : : {
2081 : 109154 : for (tree field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
2082 : : {
2083 : 99245 : if (TREE_CODE (field) == FIELD_DECL
2084 : 99245 : && DECL_INITIAL (field) != NULL_TREE)
2085 : : {
2086 : 26 : tree val = get_nsdmi (field, /*in_ctor=*/false, complain);
2087 : 26 : if (!CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init)
2088 : 26 : && find_placeholders (val))
2089 : 20 : CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init) = 1;
2090 : 26 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (init), field, val);
2091 : 26 : break;
2092 : : }
2093 : : }
2094 : :
2095 : 9935 : if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
2096 : : return 0;
2097 : : }
2098 : :
2099 : 110530 : len = CONSTRUCTOR_ELTS (init)->length ();
2100 : 110530 : if (len > 1)
2101 : : {
2102 : 3 : if (!(complain & tf_error))
2103 : : return PICFLAG_ERRONEOUS;
2104 : 3 : error ("too many initializers for %qT", type);
2105 : 3 : CONSTRUCTOR_ELTS (init)->block_remove (1, len-1);
2106 : : }
2107 : :
2108 : 110530 : ce = &(*CONSTRUCTOR_ELTS (init))[0];
2109 : :
2110 : : /* If this element specifies a field, initialize via that field. */
2111 : 110530 : if (ce->index)
2112 : : {
2113 : 110522 : if (TREE_CODE (ce->index) == FIELD_DECL)
2114 : : ;
2115 : 0 : else if (identifier_p (ce->index))
2116 : : {
2117 : : /* This can happen within a cast, see g++.dg/opt/cse2.C. */
2118 : 0 : tree name = ce->index;
2119 : 0 : tree field;
2120 : 0 : for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
2121 : 0 : if (DECL_NAME (field) == name)
2122 : : break;
2123 : 0 : if (!field)
2124 : : {
2125 : 0 : if (complain & tf_error)
2126 : 0 : error ("no field %qD found in union being initialized",
2127 : : field);
2128 : 0 : ce->value = error_mark_node;
2129 : : }
2130 : 0 : ce->index = field;
2131 : : }
2132 : : else
2133 : : {
2134 : 0 : gcc_assert (TREE_CODE (ce->index) == INTEGER_CST
2135 : : || TREE_CODE (ce->index) == RANGE_EXPR);
2136 : 0 : if (complain & tf_error)
2137 : 0 : error ("index value instead of field name in union initializer");
2138 : 0 : ce->value = error_mark_node;
2139 : : }
2140 : : }
2141 : : else
2142 : : {
2143 : : /* Find the first named field. ANSI decided in September 1990
2144 : : that only named fields count here. */
2145 : 8 : tree field = TYPE_FIELDS (type);
2146 : 90 : while (field && (!DECL_NAME (field) || TREE_CODE (field) != FIELD_DECL))
2147 : 82 : field = TREE_CHAIN (field);
2148 : 8 : if (field == NULL_TREE)
2149 : : {
2150 : 1 : if (complain & tf_error)
2151 : 1 : error ("too many initializers for %qT", type);
2152 : 1 : ce->value = error_mark_node;
2153 : : }
2154 : 8 : ce->index = field;
2155 : : }
2156 : :
2157 : 110530 : if (ce->value && ce->value != error_mark_node)
2158 : 110529 : ce->value = massage_init_elt (TREE_TYPE (ce->index), ce->value, nested,
2159 : : flags, complain);
2160 : :
2161 : : /* Propagate CONSTRUCTOR_PLACEHOLDER_BOUNDARY to outer CONSTRUCTOR. */
2162 : 110530 : if (ce->value
2163 : 110530 : && TREE_CODE (ce->value) == CONSTRUCTOR
2164 : 180094 : && CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ce->value))
2165 : : {
2166 : 0 : CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init) = 1;
2167 : 0 : CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ce->value) = 0;
2168 : : }
2169 : 110530 : return picflag_from_initializer (ce->value);
2170 : : }
2171 : :
2172 : : /* Process INIT, a constructor for a variable of aggregate type TYPE. The
2173 : : constructor is a brace-enclosed initializer, and will be modified in-place.
2174 : :
2175 : : Each element is converted to the right type through digest_init, and
2176 : : missing initializers are added following the language rules (zero-padding,
2177 : : etc.).
2178 : :
2179 : : After the execution, the initializer will have TREE_CONSTANT if all elts are
2180 : : constant, and TREE_STATIC set if, in addition, all elts are simple enough
2181 : : constants that the assembler and linker can compute them.
2182 : :
2183 : : The function returns the initializer itself, or error_mark_node in case
2184 : : of error. */
2185 : :
2186 : : static tree
2187 : 8012195 : process_init_constructor (tree type, tree init, int nested, int flags,
2188 : : tsubst_flags_t complain)
2189 : : {
2190 : 8012195 : int picflags;
2191 : :
2192 : 8012195 : gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
2193 : :
2194 : 8012195 : if (TREE_CODE (type) == ARRAY_TYPE || VECTOR_TYPE_P (type))
2195 : 348775 : picflags = process_init_constructor_array (type, init, nested, flags,
2196 : : complain);
2197 : 7663420 : else if (TREE_CODE (type) == RECORD_TYPE)
2198 : 7542981 : picflags = process_init_constructor_record (type, init, nested, flags,
2199 : : complain);
2200 : 120439 : else if (TREE_CODE (type) == UNION_TYPE)
2201 : 120439 : picflags = process_init_constructor_union (type, init, nested, flags,
2202 : : complain);
2203 : : else
2204 : 0 : gcc_unreachable ();
2205 : :
2206 : 8012195 : if (picflags & PICFLAG_ERRONEOUS)
2207 : 1090 : return error_mark_node;
2208 : :
2209 : 8011105 : TREE_TYPE (init) = type;
2210 : 8011105 : if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == NULL_TREE)
2211 : 318 : cp_complete_array_type (&TREE_TYPE (init), init, /*do_default=*/0);
2212 : 8011105 : if (picflags & PICFLAG_SIDE_EFFECTS)
2213 : : {
2214 : 71059 : TREE_CONSTANT (init) = false;
2215 : 71059 : TREE_SIDE_EFFECTS (init) = true;
2216 : : }
2217 : 7940046 : else if (picflags & PICFLAG_NOT_ALL_CONSTANT)
2218 : : {
2219 : : /* Make sure TREE_CONSTANT isn't set from build_constructor. */
2220 : 737567 : TREE_CONSTANT (init) = false;
2221 : 737567 : TREE_SIDE_EFFECTS (init) = false;
2222 : : }
2223 : : else
2224 : : {
2225 : 7202479 : TREE_CONSTANT (init) = 1;
2226 : 7202479 : TREE_SIDE_EFFECTS (init) = false;
2227 : 7202479 : if (!(picflags & PICFLAG_NOT_ALL_SIMPLE))
2228 : 7189539 : TREE_STATIC (init) = 1;
2229 : : }
2230 : 8011105 : if (picflags & PICFLAG_VEC_INIT)
2231 : : {
2232 : : /* Defer default-initialization of array elements with no corresponding
2233 : : initializer-clause until later so we can use a loop. */
2234 : 119 : TREE_TYPE (init) = init_list_type_node;
2235 : 119 : init = build_vec_init_expr (type, init, complain);
2236 : 119 : init = get_target_expr (init);
2237 : : }
2238 : : return init;
2239 : : }
2240 : :
2241 : : /* Given a structure or union value DATUM, construct and return
2242 : : the structure or union component which results from narrowing
2243 : : that value to the base specified in BASETYPE. For example, given the
2244 : : hierarchy
2245 : :
2246 : : class L { int ii; };
2247 : : class A : L { ... };
2248 : : class B : L { ... };
2249 : : class C : A, B { ... };
2250 : :
2251 : : and the declaration
2252 : :
2253 : : C x;
2254 : :
2255 : : then the expression
2256 : :
2257 : : x.A::ii refers to the ii member of the L part of
2258 : : the A part of the C object named by X. In this case,
2259 : : DATUM would be x, and BASETYPE would be A.
2260 : :
2261 : : I used to think that this was nonconformant, that the standard specified
2262 : : that first we look up ii in A, then convert x to an L& and pull out the
2263 : : ii part. But in fact, it does say that we convert x to an A&; A here
2264 : : is known as the "naming class". (jason 2000-12-19)
2265 : :
2266 : : BINFO_P points to a variable initialized either to NULL_TREE or to the
2267 : : binfo for the specific base subobject we want to convert to. */
2268 : :
2269 : : tree
2270 : 1889 : build_scoped_ref (tree datum, tree basetype, tree* binfo_p)
2271 : : {
2272 : 1889 : tree binfo;
2273 : :
2274 : 1889 : if (datum == error_mark_node)
2275 : : return error_mark_node;
2276 : 1889 : if (*binfo_p)
2277 : : binfo = *binfo_p;
2278 : : else
2279 : 1889 : binfo = lookup_base (TREE_TYPE (datum), basetype, ba_check,
2280 : : NULL, tf_warning_or_error);
2281 : :
2282 : 1889 : if (!binfo || binfo == error_mark_node)
2283 : : {
2284 : 6 : *binfo_p = NULL_TREE;
2285 : 6 : if (!binfo)
2286 : 0 : error_not_base_type (basetype, TREE_TYPE (datum));
2287 : 6 : return error_mark_node;
2288 : : }
2289 : :
2290 : 1883 : *binfo_p = binfo;
2291 : 1883 : return build_base_path (PLUS_EXPR, datum, binfo, 1,
2292 : 1883 : tf_warning_or_error);
2293 : : }
2294 : :
2295 : : /* Build a reference to an object specified by the C++ `->' operator.
2296 : : Usually this just involves dereferencing the object, but if the
2297 : : `->' operator is overloaded, then such overloads must be
2298 : : performed until an object which does not have the `->' operator
2299 : : overloaded is found. An error is reported when circular pointer
2300 : : delegation is detected. */
2301 : :
2302 : : tree
2303 : 37009215 : build_x_arrow (location_t loc, tree expr, tsubst_flags_t complain)
2304 : : {
2305 : 37009215 : tree orig_expr = expr;
2306 : 37009215 : tree type = TREE_TYPE (expr);
2307 : 37009215 : tree last_rval = NULL_TREE;
2308 : 37009215 : vec<tree, va_gc> *types_memoized = NULL;
2309 : :
2310 : 37009215 : if (type == error_mark_node)
2311 : : return error_mark_node;
2312 : :
2313 : 37009161 : if (processing_template_decl)
2314 : : {
2315 : 32740780 : tree ttype = NULL_TREE;
2316 : 32740780 : if (type && TYPE_PTR_P (type))
2317 : 26637551 : ttype = TREE_TYPE (type);
2318 : 26637551 : if (ttype && !dependent_scope_p (ttype))
2319 : : /* Pointer to current instantiation, don't treat as dependent. */;
2320 : 10171120 : else if (type_dependent_expression_p (expr))
2321 : : {
2322 : 10094820 : expr = build_min_nt_loc (loc, ARROW_EXPR, expr);
2323 : 10094820 : TREE_TYPE (expr) = ttype;
2324 : 10094820 : return expr;
2325 : : }
2326 : : }
2327 : :
2328 : 26914341 : if (MAYBE_CLASS_TYPE_P (type))
2329 : : {
2330 : 194650 : struct tinst_level *actual_inst = current_instantiation ();
2331 : 194650 : tree fn = NULL;
2332 : :
2333 : 392133 : while ((expr = build_new_op (loc, COMPONENT_REF,
2334 : : LOOKUP_NORMAL, expr, NULL_TREE, NULL_TREE,
2335 : : NULL_TREE, &fn, complain)))
2336 : : {
2337 : 197499 : if (expr == error_mark_node)
2338 : 22 : return error_mark_node;
2339 : :
2340 : : /* This provides a better instantiation backtrace in case of
2341 : : error. */
2342 : 197483 : if (fn && DECL_USE_TEMPLATE (fn))
2343 : 161842 : push_tinst_level_loc (fn,
2344 : 158989 : (current_instantiation () != actual_inst)
2345 : 2853 : ? DECL_SOURCE_LOCATION (fn)
2346 : : : input_location);
2347 : 197483 : fn = NULL;
2348 : :
2349 : 197483 : if (vec_member (TREE_TYPE (expr), types_memoized))
2350 : : {
2351 : 0 : if (complain & tf_error)
2352 : 0 : error ("circular pointer delegation detected");
2353 : 0 : return error_mark_node;
2354 : : }
2355 : :
2356 : 197483 : vec_safe_push (types_memoized, TREE_TYPE (expr));
2357 : 197483 : last_rval = expr;
2358 : : }
2359 : :
2360 : 350920 : while (current_instantiation () != actual_inst)
2361 : 156289 : pop_tinst_level ();
2362 : :
2363 : 194631 : if (last_rval == NULL_TREE)
2364 : : {
2365 : 6 : if (complain & tf_error)
2366 : 6 : error ("base operand of %<->%> has non-pointer type %qT", type);
2367 : 6 : return error_mark_node;
2368 : : }
2369 : :
2370 : 194625 : if (TYPE_REF_P (TREE_TYPE (last_rval)))
2371 : 0 : last_rval = convert_from_reference (last_rval);
2372 : : }
2373 : : else
2374 : : {
2375 : 26719691 : last_rval = decay_conversion (expr, complain);
2376 : 26719691 : if (last_rval == error_mark_node)
2377 : : return error_mark_node;
2378 : : }
2379 : :
2380 : 26914313 : if (TYPE_PTR_P (TREE_TYPE (last_rval)))
2381 : : {
2382 : 26914310 : if (processing_template_decl)
2383 : : {
2384 : 22645957 : expr = build_min (ARROW_EXPR, TREE_TYPE (TREE_TYPE (last_rval)),
2385 : : orig_expr);
2386 : 22645957 : TREE_SIDE_EFFECTS (expr) = TREE_SIDE_EFFECTS (last_rval);
2387 : 22645957 : return expr;
2388 : : }
2389 : :
2390 : 4268353 : return cp_build_indirect_ref (loc, last_rval, RO_ARROW, complain);
2391 : : }
2392 : :
2393 : 3 : if (complain & tf_error)
2394 : : {
2395 : 3 : if (types_memoized)
2396 : 0 : error ("result of %<operator->()%> yields non-pointer result");
2397 : : else
2398 : 3 : error ("base operand of %<->%> is not a pointer");
2399 : : }
2400 : 3 : return error_mark_node;
2401 : : }
2402 : :
2403 : : /* Return an expression for "DATUM .* COMPONENT". DATUM has not
2404 : : already been checked out to be of aggregate type. */
2405 : :
2406 : : tree
2407 : 111224 : build_m_component_ref (tree datum, tree component, tsubst_flags_t complain)
2408 : : {
2409 : 111224 : tree ptrmem_type;
2410 : 111224 : tree objtype;
2411 : 111224 : tree type;
2412 : 111224 : tree binfo;
2413 : 111224 : tree ctype;
2414 : :
2415 : 111224 : datum = mark_lvalue_use (datum);
2416 : 111224 : component = mark_rvalue_use (component);
2417 : :
2418 : 111224 : if (error_operand_p (datum) || error_operand_p (component))
2419 : 53 : return error_mark_node;
2420 : :
2421 : 111171 : ptrmem_type = TREE_TYPE (component);
2422 : 111171 : if (!TYPE_PTRMEM_P (ptrmem_type))
2423 : : {
2424 : 6 : if (complain & tf_error)
2425 : 3 : error ("%qE cannot be used as a member pointer, since it is of "
2426 : : "type %qT", component, ptrmem_type);
2427 : 6 : return error_mark_node;
2428 : : }
2429 : :
2430 : 111165 : objtype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
2431 : 111165 : if (! MAYBE_CLASS_TYPE_P (objtype))
2432 : : {
2433 : 20 : if (complain & tf_error)
2434 : 0 : error ("cannot apply member pointer %qE to %qE, which is of "
2435 : : "non-class type %qT", component, datum, objtype);
2436 : 20 : return error_mark_node;
2437 : : }
2438 : :
2439 : 111145 : type = TYPE_PTRMEM_POINTED_TO_TYPE (ptrmem_type);
2440 : 111145 : ctype = complete_type (TYPE_PTRMEM_CLASS_TYPE (ptrmem_type));
2441 : :
2442 : 111145 : if (!COMPLETE_TYPE_P (ctype))
2443 : : {
2444 : 103 : if (!same_type_p (ctype, objtype))
2445 : 0 : goto mismatch;
2446 : : binfo = NULL;
2447 : : }
2448 : : else
2449 : : {
2450 : 111042 : binfo = lookup_base (objtype, ctype, ba_check, NULL, complain);
2451 : :
2452 : 111042 : if (!binfo)
2453 : : {
2454 : 30 : mismatch:
2455 : 30 : if (complain & tf_error)
2456 : 0 : error ("pointer to member type %qT incompatible with object "
2457 : : "type %qT", type, objtype);
2458 : 30 : return error_mark_node;
2459 : : }
2460 : 111012 : else if (binfo == error_mark_node)
2461 : : return error_mark_node;
2462 : : }
2463 : :
2464 : 111106 : if (TYPE_PTRDATAMEM_P (ptrmem_type))
2465 : : {
2466 : 1544 : bool is_lval = real_lvalue_p (datum);
2467 : 1544 : tree ptype;
2468 : :
2469 : : /* Compute the type of the field, as described in [expr.ref].
2470 : : There's no such thing as a mutable pointer-to-member, so
2471 : : things are not as complex as they are for references to
2472 : : non-static data members. */
2473 : 1544 : type = cp_build_qualified_type (type,
2474 : 1544 : (cp_type_quals (type)
2475 : 1544 : | cp_type_quals (TREE_TYPE (datum))));
2476 : :
2477 : 1544 : datum = cp_build_addr_expr (datum, complain);
2478 : :
2479 : : /* Convert object to the correct base. */
2480 : 1544 : if (binfo)
2481 : : {
2482 : 1511 : datum = build_base_path (PLUS_EXPR, datum, binfo, 1, complain);
2483 : 1511 : if (datum == error_mark_node)
2484 : : return error_mark_node;
2485 : : }
2486 : :
2487 : : /* Build an expression for "object + offset" where offset is the
2488 : : value stored in the pointer-to-data-member. */
2489 : 1544 : ptype = build_pointer_type (type);
2490 : 1544 : datum = convert (ptype, datum);
2491 : 1544 : if (!processing_template_decl)
2492 : 1523 : datum = build2 (POINTER_PLUS_EXPR, ptype,
2493 : : datum, convert_to_ptrofftype (component));
2494 : 1544 : datum = cp_fully_fold (datum);
2495 : 1544 : datum = cp_build_fold_indirect_ref (datum);
2496 : 1544 : if (datum == error_mark_node)
2497 : : return error_mark_node;
2498 : :
2499 : : /* If the object expression was an rvalue, return an rvalue. */
2500 : 1544 : if (!is_lval)
2501 : 122 : datum = move (datum);
2502 : 1544 : return datum;
2503 : : }
2504 : : else
2505 : : {
2506 : : /* 5.5/6: In a .* expression whose object expression is an rvalue, the
2507 : : program is ill-formed if the second operand is a pointer to member
2508 : : function with ref-qualifier & (for C++20: unless its cv-qualifier-seq
2509 : : is const). In a .* expression whose object expression is an lvalue,
2510 : : the program is ill-formed if the second operand is a pointer to member
2511 : : function with ref-qualifier &&. */
2512 : 109562 : if (FUNCTION_REF_QUALIFIED (type))
2513 : : {
2514 : 138 : bool lval = lvalue_p (datum);
2515 : 138 : if (lval && FUNCTION_RVALUE_QUALIFIED (type))
2516 : : {
2517 : 15 : if (complain & tf_error)
2518 : 6 : error ("pointer-to-member-function type %qT requires an rvalue",
2519 : : ptrmem_type);
2520 : 15 : return error_mark_node;
2521 : : }
2522 : 123 : else if (!lval && !FUNCTION_RVALUE_QUALIFIED (type))
2523 : : {
2524 : 33 : if ((type_memfn_quals (type)
2525 : 33 : & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE))
2526 : : != TYPE_QUAL_CONST)
2527 : : {
2528 : 27 : if (complain & tf_error)
2529 : 24 : error ("pointer-to-member-function type %qT requires "
2530 : : "an lvalue", ptrmem_type);
2531 : 27 : return error_mark_node;
2532 : : }
2533 : 6 : else if (cxx_dialect < cxx20)
2534 : : {
2535 : 4 : if (complain & tf_warning_or_error)
2536 : 4 : pedwarn (input_location, OPT_Wpedantic,
2537 : : "pointer-to-member-function type %qT requires "
2538 : : "an lvalue before C++20", ptrmem_type);
2539 : : else
2540 : 0 : return error_mark_node;
2541 : : }
2542 : : }
2543 : : }
2544 : 109520 : return build2 (OFFSET_REF, type, datum, component);
2545 : : }
2546 : : }
2547 : :
2548 : : /* Return a tree node for the expression TYPENAME '(' PARMS ')'. */
2549 : :
2550 : : static tree
2551 : 68751992 : build_functional_cast_1 (location_t loc, tree exp, tree parms,
2552 : : tsubst_flags_t complain)
2553 : : {
2554 : : /* This is either a call to a constructor,
2555 : : or a C cast in C++'s `functional' notation. */
2556 : :
2557 : : /* The type to which we are casting. */
2558 : 68751992 : tree type;
2559 : :
2560 : 68751992 : if (error_operand_p (exp) || parms == error_mark_node)
2561 : 42245 : return error_mark_node;
2562 : :
2563 : 68709747 : if (TREE_CODE (exp) == TYPE_DECL)
2564 : : {
2565 : 39281261 : type = TREE_TYPE (exp);
2566 : :
2567 : 39281261 : if (DECL_ARTIFICIAL (exp))
2568 : 25522744 : cp_handle_deprecated_or_unavailable (type);
2569 : : }
2570 : : else
2571 : : type = exp;
2572 : :
2573 : : /* We need to check this explicitly, since value-initialization of
2574 : : arrays is allowed in other situations. */
2575 : 68709747 : if (TREE_CODE (type) == ARRAY_TYPE)
2576 : : {
2577 : 12 : if (complain & tf_error)
2578 : 6 : error_at (loc, "functional cast to array type %qT", type);
2579 : 12 : return error_mark_node;
2580 : : }
2581 : :
2582 : 68709735 : if (tree anode = type_uses_auto (type))
2583 : : {
2584 : 476947 : tree init;
2585 : 476947 : if (CLASS_PLACEHOLDER_TEMPLATE (anode))
2586 : : init = parms;
2587 : : /* C++23 auto(x). */
2588 : 188070 : else if (!AUTO_IS_DECLTYPE (anode)
2589 : 188070 : && list_length (parms) == 1)
2590 : : {
2591 : 188059 : init = TREE_VALUE (parms);
2592 : 188059 : if (is_constrained_auto (anode))
2593 : : {
2594 : 2 : if (complain & tf_error)
2595 : 2 : error_at (loc, "%<auto(x)%> cannot be constrained");
2596 : 2 : return error_mark_node;
2597 : : }
2598 : 188057 : else if (cxx_dialect < cxx23)
2599 : 18 : pedwarn (loc, OPT_Wc__23_extensions,
2600 : : "%<auto(x)%> only available with "
2601 : : "%<-std=c++23%> or %<-std=gnu++23%>");
2602 : : }
2603 : : else
2604 : : {
2605 : 11 : if (complain & tf_error)
2606 : 11 : error_at (loc, "invalid use of %qT", anode);
2607 : 11 : return error_mark_node;
2608 : : }
2609 : 476934 : type = do_auto_deduction (type, init, anode, complain,
2610 : : adc_variable_type);
2611 : 476934 : if (type == error_mark_node)
2612 : : return error_mark_node;
2613 : : }
2614 : :
2615 : 68709612 : if (processing_template_decl)
2616 : : {
2617 : 37738967 : tree t;
2618 : :
2619 : : /* Diagnose this even in a template. We could also try harder
2620 : : to give all the usual errors when the type and args are
2621 : : non-dependent... */
2622 : 37738967 : if (TYPE_REF_P (type) && !parms)
2623 : : {
2624 : 3 : if (complain & tf_error)
2625 : 3 : error_at (loc, "invalid value-initialization of reference type");
2626 : 3 : return error_mark_node;
2627 : : }
2628 : :
2629 : 37738964 : t = build_min (CAST_EXPR, type, parms);
2630 : : /* We don't know if it will or will not have side effects. */
2631 : 37738964 : TREE_SIDE_EFFECTS (t) = 1;
2632 : 37738964 : return t;
2633 : : }
2634 : :
2635 : 30970645 : if (! MAYBE_CLASS_TYPE_P (type))
2636 : : {
2637 : 28288132 : if (parms == NULL_TREE)
2638 : : {
2639 : 531523 : if (VOID_TYPE_P (type))
2640 : 4149 : return void_node;
2641 : 527374 : return build_value_init (cv_unqualified (type), complain);
2642 : : }
2643 : :
2644 : : /* This must build a C cast. */
2645 : 27756609 : parms = build_x_compound_expr_from_list (parms, ELK_FUNC_CAST, complain);
2646 : 27756609 : return cp_build_c_cast (loc, type, parms, complain);
2647 : : }
2648 : :
2649 : : /* Prepare to evaluate as a call to a constructor. If this expression
2650 : : is actually used, for example,
2651 : :
2652 : : return X (arg1, arg2, ...);
2653 : :
2654 : : then the slot being initialized will be filled in. */
2655 : :
2656 : 2682513 : if (!complete_type_or_maybe_complain (type, NULL_TREE, complain))
2657 : 18 : return error_mark_node;
2658 : 2682492 : if (abstract_virtuals_error (ACU_CAST, type, complain))
2659 : 15 : return error_mark_node;
2660 : :
2661 : : /* [expr.type.conv]
2662 : :
2663 : : If the expression list is a single-expression, the type
2664 : : conversion is equivalent (in definedness, and if defined in
2665 : : meaning) to the corresponding cast expression. */
2666 : 2682477 : if (parms && TREE_CHAIN (parms) == NULL_TREE)
2667 : 1435503 : return cp_build_c_cast (loc, type, TREE_VALUE (parms), complain);
2668 : :
2669 : : /* [expr.type.conv]
2670 : :
2671 : : The expression T(), where T is a simple-type-specifier for a
2672 : : non-array complete object type or the (possibly cv-qualified)
2673 : : void type, creates an rvalue of the specified type, which is
2674 : : value-initialized. */
2675 : :
2676 : 1246974 : if (parms == NULL_TREE)
2677 : : {
2678 : 824008 : exp = build_value_init (type, complain);
2679 : 824008 : exp = get_target_expr (exp, complain);
2680 : 824008 : return exp;
2681 : : }
2682 : :
2683 : : /* Call the constructor. */
2684 : 422966 : releasing_vec parmvec;
2685 : 1324616 : for (; parms != NULL_TREE; parms = TREE_CHAIN (parms))
2686 : 901650 : vec_safe_push (parmvec, TREE_VALUE (parms));
2687 : 422966 : exp = build_special_member_call (NULL_TREE, complete_ctor_identifier,
2688 : : &parmvec, type, LOOKUP_NORMAL, complain);
2689 : :
2690 : 422966 : if (exp == error_mark_node)
2691 : : return error_mark_node;
2692 : :
2693 : 422935 : return build_cplus_new (type, exp, complain);
2694 : 422966 : }
2695 : :
2696 : : tree
2697 : 68751992 : build_functional_cast (location_t loc, tree exp, tree parms,
2698 : : tsubst_flags_t complain)
2699 : : {
2700 : 68751992 : tree result = build_functional_cast_1 (loc, exp, parms, complain);
2701 : 68751989 : protected_set_expr_location (result, loc);
2702 : 68751989 : return result;
2703 : : }
2704 : :
2705 : :
2706 : : /* Add new exception specifier SPEC, to the LIST we currently have.
2707 : : If it's already in LIST then do nothing.
2708 : : Moan if it's bad and we're allowed to. COMPLAIN < 0 means we
2709 : : know what we're doing. */
2710 : :
2711 : : tree
2712 : 15271 : add_exception_specifier (tree list, tree spec, tsubst_flags_t complain)
2713 : : {
2714 : 15271 : bool ok;
2715 : 15271 : tree core = spec;
2716 : 15271 : bool is_ptr;
2717 : 15271 : enum diagnostics::kind diag_type = diagnostics::kind::unspecified; /* none */
2718 : :
2719 : 15271 : if (spec == error_mark_node)
2720 : : return list;
2721 : :
2722 : 15296 : gcc_assert (spec && (!list || TREE_VALUE (list)));
2723 : :
2724 : : /* [except.spec] 1, type in an exception specifier shall not be
2725 : : incomplete, or pointer or ref to incomplete other than pointer
2726 : : to cv void. */
2727 : 15257 : is_ptr = TYPE_PTR_P (core);
2728 : 15257 : if (is_ptr || TYPE_REF_P (core))
2729 : 19 : core = TREE_TYPE (core);
2730 : 15257 : if (complain < 0)
2731 : : ok = true;
2732 : 1268 : else if (VOID_TYPE_P (core))
2733 : : ok = is_ptr;
2734 : 1260 : else if (TREE_CODE (core) == TEMPLATE_TYPE_PARM)
2735 : : ok = true;
2736 : 1250 : else if (processing_template_decl)
2737 : : ok = true;
2738 : 1213 : else if (!verify_type_context (input_location, TCTX_EXCEPTIONS, core,
2739 : : !(complain & tf_error)))
2740 : 0 : return error_mark_node;
2741 : : else
2742 : : {
2743 : 1213 : ok = true;
2744 : : /* 15.4/1 says that types in an exception specifier must be complete,
2745 : : but it seems more reasonable to only require this on definitions
2746 : : and calls. So just give a pedwarn at this point; we will give an
2747 : : error later if we hit one of those two cases. */
2748 : 1213 : if (!COMPLETE_TYPE_P (complete_type (core)))
2749 : 15257 : diag_type = diagnostics::kind::pedwarn; /* pedwarn */
2750 : : }
2751 : :
2752 : 15257 : if (ok)
2753 : : {
2754 : : tree probe;
2755 : :
2756 : 15296 : for (probe = list; probe; probe = TREE_CHAIN (probe))
2757 : 47 : if (same_type_p (TREE_VALUE (probe), spec))
2758 : : break;
2759 : 15251 : if (!probe)
2760 : 15249 : list = tree_cons (NULL_TREE, spec, list);
2761 : : }
2762 : : else
2763 : : diag_type = diagnostics::kind::error; /* error */
2764 : :
2765 : 15251 : if (diag_type != diagnostics::kind::unspecified
2766 : 12 : && (complain & tf_warning_or_error))
2767 : 10 : cxx_incomplete_type_diagnostic (NULL_TREE, core, diag_type);
2768 : :
2769 : : return list;
2770 : : }
2771 : :
2772 : : /* Like nothrow_spec_p, but don't abort on deferred noexcept. */
2773 : :
2774 : : static bool
2775 : 4175608 : nothrow_spec_p_uninst (const_tree spec)
2776 : : {
2777 : 8351216 : if (DEFERRED_NOEXCEPT_SPEC_P (spec))
2778 : : return false;
2779 : 4175606 : return nothrow_spec_p (spec);
2780 : : }
2781 : :
2782 : : /* Combine the two exceptions specifier lists LIST and ADD, and return
2783 : : their union. */
2784 : :
2785 : : tree
2786 : 11866602 : merge_exception_specifiers (tree list, tree add)
2787 : : {
2788 : 11866602 : tree noex, orig_list;
2789 : :
2790 : 11866602 : if (list == error_mark_node || add == error_mark_node)
2791 : : return error_mark_node;
2792 : :
2793 : : /* No exception-specifier or noexcept(false) are less strict than
2794 : : anything else. Prefer the newer variant (LIST). */
2795 : 11866602 : if (!list || list == noexcept_false_spec)
2796 : : return list;
2797 : 4255010 : else if (!add || add == noexcept_false_spec)
2798 : : return add;
2799 : :
2800 : : /* noexcept(true) and throw() are stricter than anything else.
2801 : : As above, prefer the more recent one (LIST). */
2802 : 4136001 : if (nothrow_spec_p_uninst (add))
2803 : : return list;
2804 : :
2805 : : /* Two implicit noexcept specs (e.g. on a destructor) are equivalent. */
2806 : 39609 : if (UNEVALUATED_NOEXCEPT_SPEC_P (add)
2807 : 2 : && UNEVALUATED_NOEXCEPT_SPEC_P (list))
2808 : : return list;
2809 : : /* We should have instantiated other deferred noexcept specs by now. */
2810 : 39607 : gcc_assert (!DEFERRED_NOEXCEPT_SPEC_P (add));
2811 : :
2812 : 39607 : if (nothrow_spec_p_uninst (list))
2813 : : return add;
2814 : 39604 : noex = TREE_PURPOSE (list);
2815 : 39604 : gcc_checking_assert (!TREE_PURPOSE (add)
2816 : : || errorcount || !flag_exceptions
2817 : : || cp_tree_equal (noex, TREE_PURPOSE (add)));
2818 : :
2819 : : /* Combine the dynamic-exception-specifiers, if any. */
2820 : : orig_list = list;
2821 : 79244 : for (; add && TREE_VALUE (add); add = TREE_CHAIN (add))
2822 : : {
2823 : 41 : tree spec = TREE_VALUE (add);
2824 : : tree probe;
2825 : :
2826 : 76 : for (probe = orig_list; probe && TREE_VALUE (probe);
2827 : 12 : probe = TREE_CHAIN (probe))
2828 : 35 : if (same_type_p (TREE_VALUE (probe), spec))
2829 : : break;
2830 : 29 : if (!probe)
2831 : : {
2832 : 6 : spec = build_tree_list (NULL_TREE, spec);
2833 : 6 : TREE_CHAIN (spec) = list;
2834 : 6 : list = spec;
2835 : : }
2836 : : }
2837 : :
2838 : : /* Keep the noexcept-specifier at the beginning of the list. */
2839 : 39604 : if (noex != TREE_PURPOSE (list))
2840 : 0 : list = tree_cons (noex, TREE_VALUE (list), TREE_CHAIN (list));
2841 : :
2842 : : return list;
2843 : : }
2844 : :
2845 : : /* Subroutine of build_call. Ensure that each of the types in the
2846 : : exception specification is complete. Technically, 15.4/1 says that
2847 : : they need to be complete when we see a declaration of the function,
2848 : : but we should be able to get away with only requiring this when the
2849 : : function is defined or called. See also add_exception_specifier. */
2850 : :
2851 : : void
2852 : 127956520 : require_complete_eh_spec_types (tree fntype, tree decl)
2853 : : {
2854 : 127956520 : tree raises;
2855 : : /* Don't complain about calls to op new. */
2856 : 127956520 : if (decl && DECL_ARTIFICIAL (decl))
2857 : : return;
2858 : 179054562 : for (raises = TYPE_RAISES_EXCEPTIONS (fntype); raises;
2859 : 61378701 : raises = TREE_CHAIN (raises))
2860 : : {
2861 : 61378701 : tree type = TREE_VALUE (raises);
2862 : 61378701 : if (type && !COMPLETE_TYPE_P (type))
2863 : : {
2864 : 1 : if (decl)
2865 : 1 : error
2866 : 1 : ("call to function %qD which throws incomplete type %q#T",
2867 : : decl, type);
2868 : : else
2869 : 0 : error ("call to function which throws incomplete type %q#T",
2870 : : decl);
2871 : : }
2872 : : }
2873 : : }
2874 : :
2875 : : /* Record that any TARGET_EXPR in T are going to be elided in
2876 : : cp_gimplify_init_expr (or sooner). */
2877 : :
2878 : : void
2879 : 112465939 : set_target_expr_eliding (tree t)
2880 : : {
2881 : 114374126 : if (!t)
2882 : : return;
2883 : 113676212 : switch (TREE_CODE (t))
2884 : : {
2885 : 9908011 : case TARGET_EXPR:
2886 : 9908011 : TARGET_EXPR_ELIDING_P (t) = true;
2887 : 9908011 : break;
2888 : 920471 : case COMPOUND_EXPR:
2889 : 920471 : set_target_expr_eliding (TREE_OPERAND (t, 1));
2890 : 920471 : break;
2891 : 987716 : case COND_EXPR:
2892 : 987716 : set_target_expr_eliding (TREE_OPERAND (t, 1));
2893 : 987716 : set_target_expr_eliding (TREE_OPERAND (t, 2));
2894 : 987716 : break;
2895 : :
2896 : : default:
2897 : : break;
2898 : : }
2899 : : }
2900 : :
2901 : : /* Call the above in the process of building an INIT_EXPR. */
2902 : :
2903 : : tree
2904 : 53687782 : cp_build_init_expr (location_t loc, tree target, tree init)
2905 : : {
2906 : 53687782 : set_target_expr_eliding (init);
2907 : 53687782 : tree ie = build2_loc (loc, INIT_EXPR, TREE_TYPE (target),
2908 : : target, init);
2909 : 53687782 : TREE_SIDE_EFFECTS (ie) = true;
2910 : 53687782 : return ie;
2911 : : }
|