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