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