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