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