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