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