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