Daily bump.
[official-gcc.git] / gcc / cp / typeck2.c
blob899d60e8535d2a61e65f8a56d8ec808099d7a083
1 /* Report error messages, build initializers, and perform
2 some front-end optimizations for C++ compiler.
3 Copyright (C) 1987-2018 Free Software Foundation, Inc.
4 Hacked by Michael Tiemann (tiemann@cygnus.com)
6 This file is part of GCC.
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.
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.
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/>. */
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. */
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"
36 static tree
37 process_init_constructor (tree type, tree init, int nested,
38 tsubst_flags_t complain);
41 /* Print an error message stemming from an attempt to use
42 BASETYPE as a base class for TYPE. */
44 tree
45 error_not_base_type (tree basetype, tree type)
47 if (TREE_CODE (basetype) == FUNCTION_DECL)
48 basetype = DECL_CONTEXT (basetype);
49 error ("type %qT is not a base type for type %qT", basetype, type);
50 return error_mark_node;
53 tree
54 binfo_or_else (tree base, tree type)
56 tree binfo = lookup_base (type, base, ba_unique,
57 NULL, tf_warning_or_error);
59 if (binfo == error_mark_node)
60 return NULL_TREE;
61 else if (!binfo)
62 error_not_base_type (base, type);
63 return binfo;
66 /* According to ARM $7.1.6, "A `const' object may be initialized, but its
67 value may not be changed thereafter. */
69 void
70 cxx_readonly_error (tree arg, enum lvalue_use errstring)
73 /* This macro is used to emit diagnostics to ensure that all format
74 strings are complete sentences, visible to gettext and checked at
75 compile time. */
77 #define ERROR_FOR_ASSIGNMENT(AS, ASM, IN, DE, ARG) \
78 do { \
79 switch (errstring) \
80 { \
81 case lv_assign: \
82 error(AS, ARG); \
83 break; \
84 case lv_asm: \
85 error(ASM, ARG); \
86 break; \
87 case lv_increment: \
88 error (IN, ARG); \
89 break; \
90 case lv_decrement: \
91 error (DE, ARG); \
92 break; \
93 default: \
94 gcc_unreachable (); \
95 } \
96 } while (0)
98 /* Handle C++-specific things first. */
100 if (VAR_P (arg)
101 && DECL_LANG_SPECIFIC (arg)
102 && DECL_IN_AGGR_P (arg)
103 && !TREE_STATIC (arg))
104 ERROR_FOR_ASSIGNMENT (G_("assignment of "
105 "constant field %qD"),
106 G_("constant field %qD "
107 "used as %<asm%> output"),
108 G_("increment of "
109 "constant field %qD"),
110 G_("decrement of "
111 "constant field %qD"),
112 arg);
113 else if (INDIRECT_REF_P (arg)
114 && TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 0))) == REFERENCE_TYPE
115 && (VAR_P (TREE_OPERAND (arg, 0))
116 || TREE_CODE (TREE_OPERAND (arg, 0)) == PARM_DECL))
117 ERROR_FOR_ASSIGNMENT (G_("assignment of "
118 "read-only reference %qD"),
119 G_("read-only reference %qD "
120 "used as %<asm%> output"),
121 G_("increment of "
122 "read-only reference %qD"),
123 G_("decrement of "
124 "read-only reference %qD"),
125 TREE_OPERAND (arg, 0));
126 else
127 readonly_error (input_location, arg, errstring);
131 /* Structure that holds information about declarations whose type was
132 incomplete and we could not check whether it was abstract or not. */
134 struct GTY((chain_next ("%h.next"), for_user)) pending_abstract_type {
135 /* Declaration which we are checking for abstractness. It is either
136 a DECL node, or an IDENTIFIER_NODE if we do not have a full
137 declaration available. */
138 tree decl;
140 /* Type which will be checked for abstractness. */
141 tree type;
143 /* Kind of use in an unnamed declarator. */
144 enum abstract_class_use use;
146 /* Position of the declaration. This is only needed for IDENTIFIER_NODEs,
147 because DECLs already carry locus information. */
148 location_t locus;
150 /* Link to the next element in list. */
151 struct pending_abstract_type* next;
154 struct abstract_type_hasher : ggc_ptr_hash<pending_abstract_type>
156 typedef tree compare_type;
157 static hashval_t hash (pending_abstract_type *);
158 static bool equal (pending_abstract_type *, tree);
161 /* Compute the hash value of the node VAL. This function is used by the
162 hash table abstract_pending_vars. */
164 hashval_t
165 abstract_type_hasher::hash (pending_abstract_type *pat)
167 return (hashval_t) TYPE_UID (pat->type);
171 /* Compare node VAL1 with the type VAL2. This function is used by the
172 hash table abstract_pending_vars. */
174 bool
175 abstract_type_hasher::equal (pending_abstract_type *pat1, tree type2)
177 return (pat1->type == type2);
180 /* Hash table that maintains pending_abstract_type nodes, for which we still
181 need to check for type abstractness. The key of the table is the type
182 of the declaration. */
183 static GTY (()) hash_table<abstract_type_hasher> *abstract_pending_vars = NULL;
185 static int abstract_virtuals_error_sfinae (tree, tree, abstract_class_use, tsubst_flags_t);
187 /* This function is called after TYPE is completed, and will check if there
188 are pending declarations for which we still need to verify the abstractness
189 of TYPE, and emit a diagnostic (through abstract_virtuals_error) if TYPE
190 turned out to be incomplete. */
192 void
193 complete_type_check_abstract (tree type)
195 struct pending_abstract_type *pat;
196 location_t cur_loc = input_location;
198 gcc_assert (COMPLETE_TYPE_P (type));
200 if (!abstract_pending_vars)
201 return;
203 /* Retrieve the list of pending declarations for this type. */
204 pending_abstract_type **slot
205 = abstract_pending_vars->find_slot_with_hash (type, TYPE_UID (type),
206 NO_INSERT);
207 if (!slot)
208 return;
209 pat = *slot;
210 gcc_assert (pat);
212 /* If the type is not abstract, do not do anything. */
213 if (CLASSTYPE_PURE_VIRTUALS (type))
215 struct pending_abstract_type *prev = 0, *next;
217 /* Reverse the list to emit the errors in top-down order. */
218 for (; pat; pat = next)
220 next = pat->next;
221 pat->next = prev;
222 prev = pat;
224 pat = prev;
226 /* Go through the list, and call abstract_virtuals_error for each
227 element: it will issue a diagnostic if the type is abstract. */
228 while (pat)
230 gcc_assert (type == pat->type);
232 /* Tweak input_location so that the diagnostic appears at the correct
233 location. Notice that this is only needed if the decl is an
234 IDENTIFIER_NODE. */
235 input_location = pat->locus;
236 abstract_virtuals_error_sfinae (pat->decl, pat->type, pat->use,
237 tf_warning_or_error);
238 pat = pat->next;
242 abstract_pending_vars->clear_slot (slot);
244 input_location = cur_loc;
248 /* If TYPE has abstract virtual functions, issue an error about trying
249 to create an object of that type. DECL is the object declared, or
250 NULL_TREE if the declaration is unavailable, in which case USE specifies
251 the kind of invalid use. Returns 1 if an error occurred; zero if
252 all was well. */
254 static int
255 abstract_virtuals_error_sfinae (tree decl, tree type, abstract_class_use use,
256 tsubst_flags_t complain)
258 vec<tree, va_gc> *pure;
260 /* This function applies only to classes. Any other entity can never
261 be abstract. */
262 if (!CLASS_TYPE_P (type))
263 return 0;
264 type = TYPE_MAIN_VARIANT (type);
266 #if 0
267 /* Instantiation here seems to be required by the standard,
268 but breaks e.g. boost::bind. FIXME! */
269 /* In SFINAE, non-N3276 context, force instantiation. */
270 if (!(complain & (tf_error|tf_decltype)))
271 complete_type (type);
272 #endif
274 /* If the type is incomplete, we register it within a hash table,
275 so that we can check again once it is completed. This makes sense
276 only for objects for which we have a declaration or at least a
277 name. */
278 if (!COMPLETE_TYPE_P (type) && (complain & tf_error))
280 struct pending_abstract_type *pat;
282 gcc_assert (!decl || DECL_P (decl) || identifier_p (decl));
284 if (!abstract_pending_vars)
285 abstract_pending_vars
286 = hash_table<abstract_type_hasher>::create_ggc (31);
288 pending_abstract_type **slot
289 = abstract_pending_vars->find_slot_with_hash (type, TYPE_UID (type),
290 INSERT);
292 pat = ggc_alloc<pending_abstract_type> ();
293 pat->type = type;
294 pat->decl = decl;
295 pat->use = use;
296 pat->locus = ((decl && DECL_P (decl))
297 ? DECL_SOURCE_LOCATION (decl)
298 : input_location);
300 pat->next = *slot;
301 *slot = pat;
303 return 0;
306 if (!TYPE_SIZE (type))
307 /* TYPE is being defined, and during that time
308 CLASSTYPE_PURE_VIRTUALS holds the inline friends. */
309 return 0;
311 pure = CLASSTYPE_PURE_VIRTUALS (type);
312 if (!pure)
313 return 0;
315 if (!(complain & tf_error))
316 return 1;
318 if (decl)
320 if (VAR_P (decl))
321 error ("cannot declare variable %q+D to be of abstract "
322 "type %qT", decl, type);
323 else if (TREE_CODE (decl) == PARM_DECL)
325 if (DECL_NAME (decl))
326 error ("cannot declare parameter %q+D to be of abstract type %qT",
327 decl, type);
328 else
329 error ("cannot declare parameter to be of abstract type %qT",
330 type);
332 else if (TREE_CODE (decl) == FIELD_DECL)
333 error ("cannot declare field %q+D to be of abstract type %qT",
334 decl, type);
335 else if (TREE_CODE (decl) == FUNCTION_DECL
336 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
337 error ("invalid abstract return type for member function %q+#D", decl);
338 else if (TREE_CODE (decl) == FUNCTION_DECL)
339 error ("invalid abstract return type for function %q+#D", decl);
340 else if (identifier_p (decl))
341 /* Here we do not have location information. */
342 error ("invalid abstract type %qT for %qE", type, decl);
343 else
344 error ("invalid abstract type for %q+D", decl);
346 else switch (use)
348 case ACU_ARRAY:
349 error ("creating array of %qT, which is an abstract class type", type);
350 break;
351 case ACU_CAST:
352 error ("invalid cast to abstract class type %qT", type);
353 break;
354 case ACU_NEW:
355 error ("invalid new-expression of abstract class type %qT", type);
356 break;
357 case ACU_RETURN:
358 error ("invalid abstract return type %qT", type);
359 break;
360 case ACU_PARM:
361 error ("invalid abstract parameter type %qT", type);
362 break;
363 case ACU_THROW:
364 error ("expression of abstract class type %qT cannot "
365 "be used in throw-expression", type);
366 break;
367 case ACU_CATCH:
368 error ("cannot declare catch parameter to be of abstract "
369 "class type %qT", type);
370 break;
371 default:
372 error ("cannot allocate an object of abstract type %qT", type);
375 /* Only go through this once. */
376 if (pure->length ())
378 unsigned ix;
379 tree fn;
381 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
382 " because the following virtual functions are pure within %qT:",
383 type);
385 FOR_EACH_VEC_ELT (*pure, ix, fn)
386 if (! DECL_CLONED_FUNCTION_P (fn)
387 || DECL_COMPLETE_DESTRUCTOR_P (fn))
388 inform (DECL_SOURCE_LOCATION (fn), "\t%#qD", fn);
390 /* Now truncate the vector. This leaves it non-null, so we know
391 there are pure virtuals, but empty so we don't list them out
392 again. */
393 pure->truncate (0);
396 return 1;
400 abstract_virtuals_error_sfinae (tree decl, tree type, tsubst_flags_t complain)
402 return abstract_virtuals_error_sfinae (decl, type, ACU_UNKNOWN, complain);
406 abstract_virtuals_error_sfinae (abstract_class_use use, tree type,
407 tsubst_flags_t complain)
409 return abstract_virtuals_error_sfinae (NULL_TREE, type, use, complain);
413 /* Wrapper for the above function in the common case of wanting errors. */
416 abstract_virtuals_error (tree decl, tree type)
418 return abstract_virtuals_error_sfinae (decl, type, tf_warning_or_error);
422 abstract_virtuals_error (abstract_class_use use, tree type)
424 return abstract_virtuals_error_sfinae (use, type, tf_warning_or_error);
427 /* Print an inform about the declaration of the incomplete type TYPE. */
429 void
430 cxx_incomplete_type_inform (const_tree type)
432 if (!TYPE_MAIN_DECL (type))
433 return;
435 location_t loc = DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type));
436 tree ptype = strip_top_quals (CONST_CAST_TREE (type));
438 if (current_class_type
439 && TYPE_BEING_DEFINED (current_class_type)
440 && same_type_p (ptype, current_class_type))
441 inform (loc, "definition of %q#T is not complete until "
442 "the closing brace", ptype);
443 else if (!TYPE_TEMPLATE_INFO (ptype))
444 inform (loc, "forward declaration of %q#T", ptype);
445 else
446 inform (loc, "declaration of %q#T", ptype);
449 /* Print an error message for invalid use of an incomplete type.
450 VALUE is the expression that was used (or 0 if that isn't known)
451 and TYPE is the type that was invalid. DIAG_KIND indicates the
452 type of diagnostic (see diagnostic.def). */
454 void
455 cxx_incomplete_type_diagnostic (location_t loc, const_tree value,
456 const_tree type, diagnostic_t diag_kind)
458 bool is_decl = false, complained = false;
460 gcc_assert (diag_kind == DK_WARNING
461 || diag_kind == DK_PEDWARN
462 || diag_kind == DK_ERROR);
464 /* Avoid duplicate error message. */
465 if (TREE_CODE (type) == ERROR_MARK)
466 return;
468 if (value != 0 && (VAR_P (value)
469 || TREE_CODE (value) == PARM_DECL
470 || TREE_CODE (value) == FIELD_DECL))
472 complained = emit_diagnostic (diag_kind, DECL_SOURCE_LOCATION (value), 0,
473 "%qD has incomplete type", value);
474 is_decl = true;
476 retry:
477 /* We must print an error message. Be clever about what it says. */
479 switch (TREE_CODE (type))
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;
492 case VOID_TYPE:
493 emit_diagnostic (diag_kind, loc, 0,
494 "invalid use of %qT", type);
495 break;
497 case ARRAY_TYPE:
498 if (TYPE_DOMAIN (type))
500 type = TREE_TYPE (type);
501 goto retry;
503 emit_diagnostic (diag_kind, loc, 0,
504 "invalid use of array with unspecified bounds");
505 break;
507 case OFFSET_TYPE:
508 bad_member:
510 tree member = TREE_OPERAND (value, 1);
511 if (is_overloaded_fn (member))
512 member = get_first_fn (member);
514 if (DECL_FUNCTION_MEMBER_P (member)
515 && ! flag_ms_extensions)
516 emit_diagnostic (diag_kind, loc, 0,
517 "invalid use of member function %qD "
518 "(did you forget the %<()%> ?)", member);
519 else
520 emit_diagnostic (diag_kind, loc, 0,
521 "invalid use of member %qD "
522 "(did you forget the %<&%> ?)", member);
524 break;
526 case TEMPLATE_TYPE_PARM:
527 if (is_auto (type))
529 if (CLASS_PLACEHOLDER_TEMPLATE (type))
530 emit_diagnostic (diag_kind, loc, 0,
531 "invalid use of placeholder %qT", type);
532 else
533 emit_diagnostic (diag_kind, loc, 0,
534 "invalid use of %qT", type);
536 else
537 emit_diagnostic (diag_kind, loc, 0,
538 "invalid use of template type parameter %qT", type);
539 break;
541 case BOUND_TEMPLATE_TEMPLATE_PARM:
542 emit_diagnostic (diag_kind, loc, 0,
543 "invalid use of template template parameter %qT",
544 TYPE_NAME (type));
545 break;
547 case TYPENAME_TYPE:
548 case DECLTYPE_TYPE:
549 emit_diagnostic (diag_kind, loc, 0,
550 "invalid use of dependent type %qT", type);
551 break;
553 case LANG_TYPE:
554 if (type == init_list_type_node)
556 emit_diagnostic (diag_kind, loc, 0,
557 "invalid use of brace-enclosed initializer list");
558 break;
560 gcc_assert (type == unknown_type_node);
561 if (value && TREE_CODE (value) == COMPONENT_REF)
562 goto bad_member;
563 else if (value && TREE_CODE (value) == ADDR_EXPR)
564 emit_diagnostic (diag_kind, loc, 0,
565 "address of overloaded function with no contextual "
566 "type information");
567 else if (value && TREE_CODE (value) == OVERLOAD)
568 emit_diagnostic (diag_kind, loc, 0,
569 "overloaded function with no contextual type information");
570 else
571 emit_diagnostic (diag_kind, loc, 0,
572 "insufficient contextual information to determine type");
573 break;
575 default:
576 gcc_unreachable ();
580 /* Print an error message for invalid use of an incomplete type.
581 VALUE is the expression that was used (or 0 if that isn't known)
582 and TYPE is the type that was invalid. */
584 void
585 cxx_incomplete_type_error (location_t loc, const_tree value, const_tree type)
587 cxx_incomplete_type_diagnostic (loc, value, type, DK_ERROR);
591 /* The recursive part of split_nonconstant_init. DEST is an lvalue
592 expression to which INIT should be assigned. INIT is a CONSTRUCTOR.
593 Return true if the whole of the value was initialized by the
594 generated statements. */
596 static bool
597 split_nonconstant_init_1 (tree dest, tree init)
599 unsigned HOST_WIDE_INT idx;
600 tree field_index, value;
601 tree type = TREE_TYPE (dest);
602 tree inner_type = NULL;
603 bool array_type_p = false;
604 bool complete_p = true;
605 HOST_WIDE_INT num_split_elts = 0;
607 switch (TREE_CODE (type))
609 case ARRAY_TYPE:
610 inner_type = TREE_TYPE (type);
611 array_type_p = true;
612 if ((TREE_SIDE_EFFECTS (init)
613 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
614 || array_of_runtime_bound_p (type))
616 /* For an array, we only need/want a single cleanup region rather
617 than one per element. */
618 tree code = build_vec_init (dest, NULL_TREE, init, false, 1,
619 tf_warning_or_error);
620 add_stmt (code);
621 return true;
623 /* FALLTHRU */
625 case RECORD_TYPE:
626 case UNION_TYPE:
627 case QUAL_UNION_TYPE:
628 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), idx,
629 field_index, value)
631 /* The current implementation of this algorithm assumes that
632 the field was set for all the elements. This is usually done
633 by process_init_constructor. */
634 gcc_assert (field_index);
636 if (!array_type_p)
637 inner_type = TREE_TYPE (field_index);
639 if (TREE_CODE (value) == CONSTRUCTOR)
641 tree sub;
643 if (array_type_p)
644 sub = build4 (ARRAY_REF, inner_type, dest, field_index,
645 NULL_TREE, NULL_TREE);
646 else
647 sub = build3 (COMPONENT_REF, inner_type, dest, field_index,
648 NULL_TREE);
650 if (!split_nonconstant_init_1 (sub, value))
651 complete_p = false;
652 else
653 CONSTRUCTOR_ELTS (init)->ordered_remove (idx--);
654 num_split_elts++;
656 else if (!initializer_constant_valid_p (value, inner_type))
658 tree code;
659 tree sub;
661 /* FIXME: Ordered removal is O(1) so the whole function is
662 worst-case quadratic. This could be fixed using an aside
663 bitmap to record which elements must be removed and remove
664 them all at the same time. Or by merging
665 split_non_constant_init into process_init_constructor_array,
666 that is separating constants from non-constants while building
667 the vector. */
668 CONSTRUCTOR_ELTS (init)->ordered_remove (idx);
669 --idx;
671 if (TREE_CODE (field_index) == RANGE_EXPR)
673 /* Use build_vec_init to initialize a range. */
674 tree low = TREE_OPERAND (field_index, 0);
675 tree hi = TREE_OPERAND (field_index, 1);
676 sub = build4 (ARRAY_REF, inner_type, dest, low,
677 NULL_TREE, NULL_TREE);
678 sub = cp_build_addr_expr (sub, tf_warning_or_error);
679 tree max = size_binop (MINUS_EXPR, hi, low);
680 code = build_vec_init (sub, max, value, false, 0,
681 tf_warning_or_error);
682 add_stmt (code);
683 if (tree_fits_shwi_p (max))
684 num_split_elts += tree_to_shwi (max);
686 else
688 if (array_type_p)
689 sub = build4 (ARRAY_REF, inner_type, dest, field_index,
690 NULL_TREE, NULL_TREE);
691 else
692 sub = build3 (COMPONENT_REF, inner_type, dest, field_index,
693 NULL_TREE);
695 code = build2 (INIT_EXPR, inner_type, sub, value);
696 code = build_stmt (input_location, EXPR_STMT, code);
697 code = maybe_cleanup_point_expr_void (code);
698 add_stmt (code);
699 if (tree cleanup
700 = cxx_maybe_build_cleanup (sub, tf_warning_or_error))
701 finish_eh_cleanup (cleanup);
704 num_split_elts++;
707 break;
709 case VECTOR_TYPE:
710 if (!initializer_constant_valid_p (init, type))
712 tree code;
713 tree cons = copy_node (init);
714 CONSTRUCTOR_ELTS (init) = NULL;
715 code = build2 (MODIFY_EXPR, type, dest, cons);
716 code = build_stmt (input_location, EXPR_STMT, code);
717 add_stmt (code);
718 num_split_elts += CONSTRUCTOR_NELTS (init);
720 break;
722 default:
723 gcc_unreachable ();
726 /* The rest of the initializer is now a constant. */
727 TREE_CONSTANT (init) = 1;
729 /* We didn't split out anything. */
730 if (num_split_elts == 0)
731 return false;
733 return complete_p && complete_ctor_at_level_p (TREE_TYPE (init),
734 num_split_elts, inner_type);
737 /* A subroutine of store_init_value. Splits non-constant static
738 initializer INIT into a constant part and generates code to
739 perform the non-constant part of the initialization to DEST.
740 Returns the code for the runtime init. */
742 tree
743 split_nonconstant_init (tree dest, tree init)
745 tree code;
747 if (TREE_CODE (init) == TARGET_EXPR)
748 init = TARGET_EXPR_INITIAL (init);
749 if (TREE_CODE (init) == CONSTRUCTOR)
751 init = cp_fully_fold (init);
752 code = push_stmt_list ();
753 if (split_nonconstant_init_1 (dest, init))
754 init = NULL_TREE;
755 code = pop_stmt_list (code);
756 DECL_INITIAL (dest) = init;
757 TREE_READONLY (dest) = 0;
759 else if (TREE_CODE (init) == STRING_CST
760 && array_of_runtime_bound_p (TREE_TYPE (dest)))
761 code = build_vec_init (dest, NULL_TREE, init, /*value-init*/false,
762 /*from array*/1, tf_warning_or_error);
763 else
764 code = build2 (INIT_EXPR, TREE_TYPE (dest), dest, init);
766 return code;
769 /* Perform appropriate conversions on the initial value of a variable,
770 store it in the declaration DECL,
771 and print any error messages that are appropriate.
772 If the init is invalid, store an ERROR_MARK.
774 C++: Note that INIT might be a TREE_LIST, which would mean that it is
775 a base class initializer for some aggregate type, hopefully compatible
776 with DECL. If INIT is a single element, and DECL is an aggregate
777 type, we silently convert INIT into a TREE_LIST, allowing a constructor
778 to be called.
780 If INIT is a TREE_LIST and there is no constructor, turn INIT
781 into a CONSTRUCTOR and use standard initialization techniques.
782 Perhaps a warning should be generated?
784 Returns code to be executed if initialization could not be performed
785 for static variable. In that case, caller must emit the code. */
787 tree
788 store_init_value (tree decl, tree init, vec<tree, va_gc>** cleanups, int flags)
790 tree value, type;
792 /* If variable's type was invalidly declared, just ignore it. */
794 type = TREE_TYPE (decl);
795 if (TREE_CODE (type) == ERROR_MARK)
796 return NULL_TREE;
798 if (MAYBE_CLASS_TYPE_P (type))
800 if (TREE_CODE (init) == TREE_LIST)
802 error ("constructor syntax used, but no constructor declared "
803 "for type %qT", type);
804 init = build_constructor_from_list (init_list_type_node, nreverse (init));
808 /* End of special C++ code. */
810 if (flags & LOOKUP_ALREADY_DIGESTED)
811 value = init;
812 else
813 /* Digest the specified initializer into an expression. */
814 value = digest_init_flags (type, init, flags, tf_warning_or_error);
816 value = extend_ref_init_temps (decl, value, cleanups);
818 /* In C++11 constant expression is a semantic, not syntactic, property.
819 In C++98, make sure that what we thought was a constant expression at
820 template definition time is still constant and otherwise perform this
821 as optimization, e.g. to fold SIZEOF_EXPRs in the initializer. */
822 if (decl_maybe_constant_var_p (decl) || TREE_STATIC (decl))
824 bool const_init;
825 value = instantiate_non_dependent_expr (value);
826 if (DECL_DECLARED_CONSTEXPR_P (decl)
827 || (DECL_IN_AGGR_P (decl) && !DECL_VAR_DECLARED_INLINE_P (decl)))
829 /* Diagnose a non-constant initializer for constexpr. */
830 if (!require_constant_expression (value))
831 value = error_mark_node;
832 else
833 value = cxx_constant_value (value, decl);
835 value = maybe_constant_init (value, decl);
836 if (TREE_CODE (value) == CONSTRUCTOR && cp_has_mutable_p (type))
837 /* Poison this CONSTRUCTOR so it can't be copied to another
838 constexpr variable. */
839 CONSTRUCTOR_MUTABLE_POISON (value) = true;
840 const_init = (reduced_constant_expression_p (value)
841 || error_operand_p (value));
842 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = const_init;
843 /* FIXME setting TREE_CONSTANT on refs breaks the back end. */
844 if (TREE_CODE (type) != REFERENCE_TYPE)
845 TREE_CONSTANT (decl) = const_init && decl_maybe_constant_var_p (decl);
847 value = cp_fully_fold (value);
849 /* Handle aggregate NSDMI in non-constant initializers, too. */
850 value = replace_placeholders (value, decl);
852 /* DECL may change value; purge caches. */
853 clear_cv_and_fold_caches ();
855 /* If the initializer is not a constant, fill in DECL_INITIAL with
856 the bits that are constant, and then return an expression that
857 will perform the dynamic initialization. */
858 if (value != error_mark_node
859 && (TREE_SIDE_EFFECTS (value)
860 || array_of_runtime_bound_p (type)
861 || ! reduced_constant_expression_p (value)))
862 return split_nonconstant_init (decl, value);
863 /* If the value is a constant, just put it in DECL_INITIAL. If DECL
864 is an automatic variable, the middle end will turn this into a
865 dynamic initialization later. */
866 DECL_INITIAL (decl) = value;
867 return NULL_TREE;
871 /* Give diagnostic about narrowing conversions within { }. */
873 bool
874 check_narrowing (tree type, tree init, tsubst_flags_t complain)
876 tree ftype = unlowered_expr_type (init);
877 bool ok = true;
878 REAL_VALUE_TYPE d;
880 if (((!warn_narrowing || !(complain & tf_warning))
881 && cxx_dialect == cxx98)
882 || !ARITHMETIC_TYPE_P (type))
883 return ok;
885 if (BRACE_ENCLOSED_INITIALIZER_P (init)
886 && TREE_CODE (type) == COMPLEX_TYPE)
888 tree elttype = TREE_TYPE (type);
889 if (CONSTRUCTOR_NELTS (init) > 0)
890 ok &= check_narrowing (elttype, CONSTRUCTOR_ELT (init, 0)->value,
891 complain);
892 if (CONSTRUCTOR_NELTS (init) > 1)
893 ok &= check_narrowing (elttype, CONSTRUCTOR_ELT (init, 1)->value,
894 complain);
895 return ok;
898 init = fold_non_dependent_expr (init);
900 if (TREE_CODE (type) == INTEGER_TYPE
901 && TREE_CODE (ftype) == REAL_TYPE)
902 ok = false;
903 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype)
904 && CP_INTEGRAL_TYPE_P (type))
906 if (TREE_CODE (ftype) == ENUMERAL_TYPE)
907 /* Check for narrowing based on the values of the enumeration. */
908 ftype = ENUM_UNDERLYING_TYPE (ftype);
909 if ((tree_int_cst_lt (TYPE_MAX_VALUE (type),
910 TYPE_MAX_VALUE (ftype))
911 || tree_int_cst_lt (TYPE_MIN_VALUE (ftype),
912 TYPE_MIN_VALUE (type)))
913 && (TREE_CODE (init) != INTEGER_CST
914 || !int_fits_type_p (init, type)))
915 ok = false;
917 else if (TREE_CODE (ftype) == REAL_TYPE
918 && TREE_CODE (type) == REAL_TYPE)
920 if (TYPE_PRECISION (type) < TYPE_PRECISION (ftype))
922 if (TREE_CODE (init) == REAL_CST)
924 /* Issue 703: Loss of precision is OK as long as the value is
925 within the representable range of the new type. */
926 REAL_VALUE_TYPE r;
927 d = TREE_REAL_CST (init);
928 real_convert (&r, TYPE_MODE (type), &d);
929 if (real_isinf (&r))
930 ok = false;
932 else
933 ok = false;
936 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype)
937 && TREE_CODE (type) == REAL_TYPE)
939 ok = false;
940 if (TREE_CODE (init) == INTEGER_CST)
942 d = real_value_from_int_cst (0, init);
943 if (exact_real_truncate (TYPE_MODE (type), &d))
944 ok = true;
948 bool almost_ok = ok;
949 if (!ok && !CONSTANT_CLASS_P (init) && (complain & tf_warning_or_error))
951 tree folded = cp_fully_fold (init);
952 if (TREE_CONSTANT (folded) && check_narrowing (type, folded, tf_none))
953 almost_ok = true;
956 if (!ok)
958 location_t loc = EXPR_LOC_OR_LOC (init, input_location);
959 if (cxx_dialect == cxx98)
961 if (complain & tf_warning)
962 warning_at (loc, OPT_Wnarrowing, "narrowing conversion of %qE "
963 "from %qH to %qI inside { } is ill-formed in C++11",
964 init, ftype, type);
965 ok = true;
967 else if (!CONSTANT_CLASS_P (init))
969 if (complain & tf_warning_or_error)
971 if ((!almost_ok || pedantic)
972 && pedwarn (loc, OPT_Wnarrowing,
973 "narrowing conversion of %qE "
974 "from %qH to %qI inside { }",
975 init, ftype, type)
976 && almost_ok)
977 inform (loc, " the expression has a constant value but is not "
978 "a C++ constant-expression");
979 ok = true;
982 else if (complain & tf_error)
984 int savederrorcount = errorcount;
985 global_dc->pedantic_errors = 1;
986 pedwarn (loc, OPT_Wnarrowing,
987 "narrowing conversion of %qE from %qH to %qI "
988 "inside { }", init, ftype, type);
989 if (errorcount == savederrorcount)
990 ok = true;
991 global_dc->pedantic_errors = flag_pedantic_errors;
995 return ok;
998 /* Process the initializer INIT for a variable of type TYPE, emitting
999 diagnostics for invalid initializers and converting the initializer as
1000 appropriate.
1002 For aggregate types, it assumes that reshape_init has already run, thus the
1003 initializer will have the right shape (brace elision has been undone).
1005 NESTED is non-zero iff we are being called for an element of a CONSTRUCTOR,
1006 2 iff the element of a CONSTRUCTOR is inside another CONSTRUCTOR. */
1008 static tree
1009 digest_init_r (tree type, tree init, int nested, int flags,
1010 tsubst_flags_t complain)
1012 enum tree_code code = TREE_CODE (type);
1014 if (error_operand_p (init))
1015 return error_mark_node;
1017 gcc_assert (init);
1019 /* We must strip the outermost array type when completing the type,
1020 because the its bounds might be incomplete at the moment. */
1021 if (!complete_type_or_maybe_complain (code == ARRAY_TYPE
1022 ? TREE_TYPE (type) : type, NULL_TREE,
1023 complain))
1024 return error_mark_node;
1026 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue
1027 (g++.old-deja/g++.law/casts2.C). */
1028 if (TREE_CODE (init) == NON_LVALUE_EXPR)
1029 init = TREE_OPERAND (init, 0);
1031 location_t loc = EXPR_LOC_OR_LOC (init, input_location);
1033 /* Initialization of an array of chars from a string constant. The initializer
1034 can be optionally enclosed in braces, but reshape_init has already removed
1035 them if they were present. */
1036 if (code == ARRAY_TYPE)
1038 if (nested && !TYPE_DOMAIN (type))
1039 /* C++ flexible array members have a null domain. */
1040 pedwarn (loc, OPT_Wpedantic,
1041 "initialization of a flexible array member");
1043 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
1044 if (char_type_p (typ1)
1045 /*&& init */
1046 && TREE_CODE (init) == STRING_CST)
1048 tree char_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (init)));
1050 if (TYPE_PRECISION (typ1) == BITS_PER_UNIT)
1052 if (char_type != char_type_node)
1054 if (complain & tf_error)
1055 error_at (loc, "char-array initialized from wide string");
1056 return error_mark_node;
1059 else
1061 if (char_type == char_type_node)
1063 if (complain & tf_error)
1064 error_at (loc,
1065 "int-array initialized from non-wide string");
1066 return error_mark_node;
1068 else if (char_type != typ1)
1070 if (complain & tf_error)
1071 error_at (loc, "int-array initialized from incompatible "
1072 "wide string");
1073 return error_mark_node;
1077 if (nested == 2 && !TYPE_DOMAIN (type))
1079 if (complain & tf_error)
1080 error_at (loc, "initialization of flexible array member "
1081 "in a nested context");
1082 return error_mark_node;
1085 if (type != TREE_TYPE (init)
1086 && !variably_modified_type_p (type, NULL_TREE))
1088 init = copy_node (init);
1089 TREE_TYPE (init) = type;
1091 if (TYPE_DOMAIN (type) && TREE_CONSTANT (TYPE_SIZE (type)))
1093 /* Not a flexible array member. */
1094 int size = TREE_INT_CST_LOW (TYPE_SIZE (type));
1095 size = (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
1096 /* In C it is ok to subtract 1 from the length of the string
1097 because it's ok to ignore the terminating null char that is
1098 counted in the length of the constant, but in C++ this would
1099 be invalid. */
1100 if (size < TREE_STRING_LENGTH (init))
1101 permerror (loc, "initializer-string for array "
1102 "of chars is too long");
1104 return init;
1108 /* Handle scalar types (including conversions) and references. */
1109 if ((code != COMPLEX_TYPE || BRACE_ENCLOSED_INITIALIZER_P (init))
1110 && (SCALAR_TYPE_P (type) || code == REFERENCE_TYPE))
1112 if (nested)
1113 flags |= LOOKUP_NO_NARROWING;
1114 init = convert_for_initialization (0, type, init, flags,
1115 ICR_INIT, NULL_TREE, 0,
1116 complain);
1118 return init;
1121 /* Come here only for aggregates: records, arrays, unions, complex numbers
1122 and vectors. */
1123 gcc_assert (code == ARRAY_TYPE
1124 || VECTOR_TYPE_P (type)
1125 || code == RECORD_TYPE
1126 || code == UNION_TYPE
1127 || code == COMPLEX_TYPE);
1129 /* "If T is a class type and the initializer list has a single
1130 element of type cv U, where U is T or a class derived from T,
1131 the object is initialized from that element." */
1132 if (flag_checking
1133 && cxx_dialect >= cxx11
1134 && BRACE_ENCLOSED_INITIALIZER_P (init)
1135 && CONSTRUCTOR_NELTS (init) == 1
1136 && ((CLASS_TYPE_P (type) && !CLASSTYPE_NON_AGGREGATE (type))
1137 || VECTOR_TYPE_P (type)))
1139 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
1140 if (reference_related_p (type, TREE_TYPE (elt)))
1141 /* We should have fixed this in reshape_init. */
1142 gcc_unreachable ();
1145 if (BRACE_ENCLOSED_INITIALIZER_P (init)
1146 && !TYPE_NON_AGGREGATE_CLASS (type))
1147 return process_init_constructor (type, init, nested, complain);
1148 else
1150 if (COMPOUND_LITERAL_P (init) && code == ARRAY_TYPE)
1152 if (complain & tf_error)
1153 error_at (loc, "cannot initialize aggregate of type %qT with "
1154 "a compound literal", type);
1156 return error_mark_node;
1159 if (code == ARRAY_TYPE
1160 && !BRACE_ENCLOSED_INITIALIZER_P (init))
1162 /* Allow the result of build_array_copy and of
1163 build_value_init_noctor. */
1164 if ((TREE_CODE (init) == VEC_INIT_EXPR
1165 || TREE_CODE (init) == CONSTRUCTOR)
1166 && (same_type_ignoring_top_level_qualifiers_p
1167 (type, TREE_TYPE (init))))
1168 return init;
1170 if (complain & tf_error)
1171 error_at (loc, "array must be initialized with a brace-enclosed"
1172 " initializer");
1173 return error_mark_node;
1176 return convert_for_initialization (NULL_TREE, type, init,
1177 flags,
1178 ICR_INIT, NULL_TREE, 0,
1179 complain);
1183 tree
1184 digest_init (tree type, tree init, tsubst_flags_t complain)
1186 return digest_init_r (type, init, 0, LOOKUP_IMPLICIT, complain);
1189 tree
1190 digest_init_flags (tree type, tree init, int flags, tsubst_flags_t complain)
1192 return digest_init_r (type, init, 0, flags, complain);
1195 /* Process the initializer INIT for an NSDMI DECL (a FIELD_DECL). */
1196 tree
1197 digest_nsdmi_init (tree decl, tree init, tsubst_flags_t complain)
1199 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
1201 tree type = TREE_TYPE (decl);
1202 int flags = LOOKUP_IMPLICIT;
1203 if (DIRECT_LIST_INIT_P (init))
1204 flags = LOOKUP_NORMAL;
1205 if (BRACE_ENCLOSED_INITIALIZER_P (init)
1206 && CP_AGGREGATE_TYPE_P (type))
1207 init = reshape_init (type, init, complain);
1208 init = digest_init_flags (type, init, flags, complain);
1209 if (TREE_CODE (init) == TARGET_EXPR)
1210 /* This represents the whole initialization. */
1211 TARGET_EXPR_DIRECT_INIT_P (init) = true;
1212 return init;
1215 /* Set of flags used within process_init_constructor to describe the
1216 initializers. */
1217 #define PICFLAG_ERRONEOUS 1
1218 #define PICFLAG_NOT_ALL_CONSTANT 2
1219 #define PICFLAG_NOT_ALL_SIMPLE 4
1220 #define PICFLAG_SIDE_EFFECTS 8
1222 /* Given an initializer INIT, return the flag (PICFLAG_*) which better
1223 describe it. */
1225 static int
1226 picflag_from_initializer (tree init)
1228 if (init == error_mark_node)
1229 return PICFLAG_ERRONEOUS;
1230 else if (!TREE_CONSTANT (init))
1232 if (TREE_SIDE_EFFECTS (init))
1233 return PICFLAG_SIDE_EFFECTS;
1234 else
1235 return PICFLAG_NOT_ALL_CONSTANT;
1237 else if (!initializer_constant_valid_p (init, TREE_TYPE (init)))
1238 return PICFLAG_NOT_ALL_SIMPLE;
1239 return 0;
1242 /* Adjust INIT for going into a CONSTRUCTOR. */
1244 static tree
1245 massage_init_elt (tree type, tree init, int nested, tsubst_flags_t complain)
1247 init = digest_init_r (type, init, nested ? 2 : 1, LOOKUP_IMPLICIT, complain);
1248 /* Strip a simple TARGET_EXPR when we know this is an initializer. */
1249 if (SIMPLE_TARGET_EXPR_P (init))
1250 init = TARGET_EXPR_INITIAL (init);
1251 /* When we defer constant folding within a statement, we may want to
1252 defer this folding as well. */
1253 tree t = fold_non_dependent_expr (init);
1254 t = maybe_constant_init (t);
1255 if (TREE_CONSTANT (t))
1256 init = t;
1257 return init;
1260 /* Subroutine of process_init_constructor, which will process an initializer
1261 INIT for an array or vector of type TYPE. Returns the flags (PICFLAG_*)
1262 which describe the initializers. */
1264 static int
1265 process_init_constructor_array (tree type, tree init, int nested,
1266 tsubst_flags_t complain)
1268 unsigned HOST_WIDE_INT i, len = 0;
1269 int flags = 0;
1270 bool unbounded = false;
1271 constructor_elt *ce;
1272 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (init);
1274 gcc_assert (TREE_CODE (type) == ARRAY_TYPE
1275 || VECTOR_TYPE_P (type));
1277 if (TREE_CODE (type) == ARRAY_TYPE)
1279 /* C++ flexible array members have a null domain. */
1280 tree domain = TYPE_DOMAIN (type);
1281 if (domain && TREE_CONSTANT (TYPE_MAX_VALUE (domain)))
1282 len = wi::ext (wi::to_offset (TYPE_MAX_VALUE (domain))
1283 - wi::to_offset (TYPE_MIN_VALUE (domain)) + 1,
1284 TYPE_PRECISION (TREE_TYPE (domain)),
1285 TYPE_SIGN (TREE_TYPE (domain))).to_uhwi ();
1286 else
1287 unbounded = true; /* Take as many as there are. */
1289 if (nested == 2 && !domain && !vec_safe_is_empty (v))
1291 if (complain & tf_error)
1292 error_at (EXPR_LOC_OR_LOC (init, input_location),
1293 "initialization of flexible array member "
1294 "in a nested context");
1295 return PICFLAG_ERRONEOUS;
1298 else
1299 /* Vectors are like simple fixed-size arrays. */
1300 unbounded = !TYPE_VECTOR_SUBPARTS (type).is_constant (&len);
1302 /* There must not be more initializers than needed. */
1303 if (!unbounded && vec_safe_length (v) > len)
1305 if (complain & tf_error)
1306 error ("too many initializers for %qT", type);
1307 else
1308 return PICFLAG_ERRONEOUS;
1311 FOR_EACH_VEC_SAFE_ELT (v, i, ce)
1313 if (!ce->index)
1314 ce->index = size_int (i);
1315 else if (!check_array_designated_initializer (ce, i))
1316 ce->index = error_mark_node;
1317 gcc_assert (ce->value);
1318 ce->value
1319 = massage_init_elt (TREE_TYPE (type), ce->value, nested, complain);
1321 if (ce->value != error_mark_node)
1322 gcc_assert (same_type_ignoring_top_level_qualifiers_p
1323 (TREE_TYPE (type), TREE_TYPE (ce->value)));
1325 flags |= picflag_from_initializer (ce->value);
1328 /* No more initializers. If the array is unbounded, we are done. Otherwise,
1329 we must add initializers ourselves. */
1330 if (!unbounded)
1331 for (; i < len; ++i)
1333 tree next;
1335 if (type_build_ctor_call (TREE_TYPE (type)))
1337 /* If this type needs constructors run for default-initialization,
1338 we can't rely on the back end to do it for us, so make the
1339 initialization explicit by list-initializing from T{}. */
1340 next = build_constructor (init_list_type_node, NULL);
1341 next = massage_init_elt (TREE_TYPE (type), next, nested, complain);
1342 if (initializer_zerop (next))
1343 /* The default zero-initialization is fine for us; don't
1344 add anything to the CONSTRUCTOR. */
1345 next = NULL_TREE;
1347 else if (!zero_init_p (TREE_TYPE (type)))
1348 next = build_zero_init (TREE_TYPE (type),
1349 /*nelts=*/NULL_TREE,
1350 /*static_storage_p=*/false);
1351 else
1352 /* The default zero-initialization is fine for us; don't
1353 add anything to the CONSTRUCTOR. */
1354 next = NULL_TREE;
1356 if (next)
1358 flags |= picflag_from_initializer (next);
1359 CONSTRUCTOR_APPEND_ELT (v, size_int (i), next);
1363 CONSTRUCTOR_ELTS (init) = v;
1364 return flags;
1367 /* Subroutine of process_init_constructor, which will process an initializer
1368 INIT for a class of type TYPE. Returns the flags (PICFLAG_*) which describe
1369 the initializers. */
1371 static int
1372 process_init_constructor_record (tree type, tree init, int nested,
1373 tsubst_flags_t complain)
1375 vec<constructor_elt, va_gc> *v = NULL;
1376 tree field;
1377 int skipped = 0;
1379 gcc_assert (TREE_CODE (type) == RECORD_TYPE);
1380 gcc_assert (!CLASSTYPE_VBASECLASSES (type));
1381 gcc_assert (!TYPE_BINFO (type)
1382 || cxx_dialect >= cxx17
1383 || !BINFO_N_BASE_BINFOS (TYPE_BINFO (type)));
1384 gcc_assert (!TYPE_POLYMORPHIC_P (type));
1386 restart:
1387 int flags = 0;
1388 unsigned HOST_WIDE_INT idx = 0;
1389 int designator_skip = -1;
1390 /* Generally, we will always have an index for each initializer (which is
1391 a FIELD_DECL, put by reshape_init), but compound literals don't go trough
1392 reshape_init. So we need to handle both cases. */
1393 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
1395 tree next;
1396 tree type;
1398 if (TREE_CODE (field) != FIELD_DECL
1399 || (DECL_ARTIFICIAL (field)
1400 && !(cxx_dialect >= cxx17 && DECL_FIELD_IS_BASE (field))))
1401 continue;
1403 if (DECL_UNNAMED_BIT_FIELD (field))
1404 continue;
1406 /* If this is a bitfield, first convert to the declared type. */
1407 type = TREE_TYPE (field);
1408 if (DECL_BIT_FIELD_TYPE (field))
1409 type = DECL_BIT_FIELD_TYPE (field);
1410 if (type == error_mark_node)
1411 return PICFLAG_ERRONEOUS;
1413 next = NULL_TREE;
1414 if (idx < CONSTRUCTOR_NELTS (init))
1416 constructor_elt *ce = &(*CONSTRUCTOR_ELTS (init))[idx];
1417 if (ce->index)
1419 /* We can have either a FIELD_DECL or an IDENTIFIER_NODE. The
1420 latter case can happen in templates where lookup has to be
1421 deferred. */
1422 gcc_assert (TREE_CODE (ce->index) == FIELD_DECL
1423 || identifier_p (ce->index));
1424 if (ce->index == field || ce->index == DECL_NAME (field))
1425 next = ce->value;
1426 else if (ANON_AGGR_TYPE_P (type)
1427 && search_anon_aggr (type,
1428 TREE_CODE (ce->index) == FIELD_DECL
1429 ? DECL_NAME (ce->index)
1430 : ce->index))
1431 /* If the element is an anonymous union object and the
1432 initializer list is a designated-initializer-list, the
1433 anonymous union object is initialized by the
1434 designated-initializer-list { D }, where D is the
1435 designated-initializer-clause naming a member of the
1436 anonymous union object. */
1437 next = build_constructor_single (type, ce->index, ce->value);
1438 else
1440 ce = NULL;
1441 if (designator_skip == -1)
1442 designator_skip = 1;
1445 else
1447 designator_skip = 0;
1448 next = ce->value;
1451 if (ce)
1453 gcc_assert (ce->value);
1454 next = massage_init_elt (type, next, nested, complain);
1455 ++idx;
1458 if (next)
1459 /* Already handled above. */;
1460 else if (DECL_INITIAL (field))
1462 if (skipped > 0)
1464 /* We're using an NSDMI past a field with implicit
1465 zero-init. Go back and make it explicit. */
1466 skipped = -1;
1467 vec_safe_truncate (v, 0);
1468 goto restart;
1470 /* C++14 aggregate NSDMI. */
1471 next = get_nsdmi (field, /*ctor*/false, complain);
1473 else if (type_build_ctor_call (TREE_TYPE (field)))
1475 /* If this type needs constructors run for
1476 default-initialization, we can't rely on the back end to do it
1477 for us, so build up TARGET_EXPRs. If the type in question is
1478 a class, just build one up; if it's an array, recurse. */
1479 next = build_constructor (init_list_type_node, NULL);
1480 next = massage_init_elt (TREE_TYPE (field), next, nested, complain);
1482 /* Warn when some struct elements are implicitly initialized. */
1483 if ((complain & tf_warning)
1484 && !EMPTY_CONSTRUCTOR_P (init))
1485 warning (OPT_Wmissing_field_initializers,
1486 "missing initializer for member %qD", field);
1488 else
1490 const_tree fldtype = TREE_TYPE (field);
1491 if (TREE_CODE (fldtype) == REFERENCE_TYPE)
1493 if (complain & tf_error)
1494 error ("member %qD is uninitialized reference", field);
1495 else
1496 return PICFLAG_ERRONEOUS;
1498 else if (CLASSTYPE_REF_FIELDS_NEED_INIT (fldtype))
1500 if (complain & tf_error)
1501 error ("member %qD with uninitialized reference fields", field);
1502 else
1503 return PICFLAG_ERRONEOUS;
1506 /* Warn when some struct elements are implicitly initialized
1507 to zero. However, avoid issuing the warning for flexible
1508 array members since they need not have any elements. */
1509 if ((TREE_CODE (fldtype) != ARRAY_TYPE || TYPE_DOMAIN (fldtype))
1510 && (complain & tf_warning)
1511 && !EMPTY_CONSTRUCTOR_P (init))
1512 warning (OPT_Wmissing_field_initializers,
1513 "missing initializer for member %qD", field);
1515 if (!zero_init_p (fldtype)
1516 || skipped < 0)
1517 next = build_zero_init (TREE_TYPE (field), /*nelts=*/NULL_TREE,
1518 /*static_storage_p=*/false);
1519 else
1521 /* The default zero-initialization is fine for us; don't
1522 add anything to the CONSTRUCTOR. */
1523 skipped = 1;
1524 continue;
1528 /* If this is a bitfield, now convert to the lowered type. */
1529 if (type != TREE_TYPE (field))
1530 next = cp_convert_and_check (TREE_TYPE (field), next, complain);
1531 flags |= picflag_from_initializer (next);
1532 CONSTRUCTOR_APPEND_ELT (v, field, next);
1535 if (idx < CONSTRUCTOR_NELTS (init))
1537 if (complain & tf_error)
1539 constructor_elt *ce = &(*CONSTRUCTOR_ELTS (init))[idx];
1540 /* For better diagnostics, try to find out if it is really
1541 the case of too many initializers or if designators are
1542 in incorrect order. */
1543 if (designator_skip == 1 && ce->index)
1545 gcc_assert (TREE_CODE (ce->index) == FIELD_DECL
1546 || identifier_p (ce->index));
1547 for (field = TYPE_FIELDS (type);
1548 field; field = DECL_CHAIN (field))
1550 if (TREE_CODE (field) != FIELD_DECL
1551 || (DECL_ARTIFICIAL (field)
1552 && !(cxx_dialect >= cxx17
1553 && DECL_FIELD_IS_BASE (field))))
1554 continue;
1556 if (DECL_UNNAMED_BIT_FIELD (field))
1557 continue;
1559 if (ce->index == field || ce->index == DECL_NAME (field))
1560 break;
1561 if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1563 tree t
1564 = search_anon_aggr (TREE_TYPE (field),
1565 TREE_CODE (ce->index) == FIELD_DECL
1566 ? DECL_NAME (ce->index)
1567 : ce->index);
1568 if (t)
1570 field = t;
1571 break;
1576 if (field)
1577 error ("designator order for field %qD does not match declaration "
1578 "order in %qT", field, type);
1579 else
1580 error ("too many initializers for %qT", type);
1582 else
1583 return PICFLAG_ERRONEOUS;
1586 CONSTRUCTOR_ELTS (init) = v;
1587 return flags;
1590 /* Subroutine of process_init_constructor, which will process a single
1591 initializer INIT for a union of type TYPE. Returns the flags (PICFLAG_*)
1592 which describe the initializer. */
1594 static int
1595 process_init_constructor_union (tree type, tree init, int nested,
1596 tsubst_flags_t complain)
1598 constructor_elt *ce;
1599 int len;
1601 /* If the initializer was empty, use the union's NSDMI if it has one.
1602 Otherwise use default zero initialization. */
1603 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
1605 for (tree field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1607 if (TREE_CODE (field) == FIELD_DECL
1608 && DECL_INITIAL (field) != NULL_TREE)
1610 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (init),
1611 field,
1612 get_nsdmi (field, /*in_ctor=*/false,
1613 complain));
1614 break;
1618 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
1619 return 0;
1622 len = CONSTRUCTOR_ELTS (init)->length ();
1623 if (len > 1)
1625 if (!(complain & tf_error))
1626 return PICFLAG_ERRONEOUS;
1627 error ("too many initializers for %qT", type);
1628 CONSTRUCTOR_ELTS (init)->block_remove (1, len-1);
1631 ce = &(*CONSTRUCTOR_ELTS (init))[0];
1633 /* If this element specifies a field, initialize via that field. */
1634 if (ce->index)
1636 if (TREE_CODE (ce->index) == FIELD_DECL)
1638 else if (identifier_p (ce->index))
1640 /* This can happen within a cast, see g++.dg/opt/cse2.C. */
1641 tree name = ce->index;
1642 tree field;
1643 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1644 if (DECL_NAME (field) == name)
1645 break;
1646 if (!field)
1648 if (complain & tf_error)
1649 error ("no field %qD found in union being initialized",
1650 field);
1651 ce->value = error_mark_node;
1653 ce->index = field;
1655 else
1657 gcc_assert (TREE_CODE (ce->index) == INTEGER_CST
1658 || TREE_CODE (ce->index) == RANGE_EXPR);
1659 if (complain & tf_error)
1660 error ("index value instead of field name in union initializer");
1661 ce->value = error_mark_node;
1664 else
1666 /* Find the first named field. ANSI decided in September 1990
1667 that only named fields count here. */
1668 tree field = TYPE_FIELDS (type);
1669 while (field && (!DECL_NAME (field) || TREE_CODE (field) != FIELD_DECL))
1670 field = TREE_CHAIN (field);
1671 if (field == NULL_TREE)
1673 if (complain & tf_error)
1674 error ("too many initializers for %qT", type);
1675 ce->value = error_mark_node;
1677 ce->index = field;
1680 if (ce->value && ce->value != error_mark_node)
1681 ce->value = massage_init_elt (TREE_TYPE (ce->index), ce->value, nested,
1682 complain);
1684 return picflag_from_initializer (ce->value);
1687 /* Process INIT, a constructor for a variable of aggregate type TYPE. The
1688 constructor is a brace-enclosed initializer, and will be modified in-place.
1690 Each element is converted to the right type through digest_init, and
1691 missing initializers are added following the language rules (zero-padding,
1692 etc.).
1694 After the execution, the initializer will have TREE_CONSTANT if all elts are
1695 constant, and TREE_STATIC set if, in addition, all elts are simple enough
1696 constants that the assembler and linker can compute them.
1698 The function returns the initializer itself, or error_mark_node in case
1699 of error. */
1701 static tree
1702 process_init_constructor (tree type, tree init, int nested,
1703 tsubst_flags_t complain)
1705 int flags;
1707 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
1709 if (TREE_CODE (type) == ARRAY_TYPE || VECTOR_TYPE_P (type))
1710 flags = process_init_constructor_array (type, init, nested, complain);
1711 else if (TREE_CODE (type) == RECORD_TYPE)
1712 flags = process_init_constructor_record (type, init, nested, complain);
1713 else if (TREE_CODE (type) == UNION_TYPE)
1714 flags = process_init_constructor_union (type, init, nested, complain);
1715 else
1716 gcc_unreachable ();
1718 if (flags & PICFLAG_ERRONEOUS)
1719 return error_mark_node;
1721 TREE_TYPE (init) = type;
1722 if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == NULL_TREE)
1723 cp_complete_array_type (&TREE_TYPE (init), init, /*do_default=*/0);
1724 if (flags & PICFLAG_SIDE_EFFECTS)
1726 TREE_CONSTANT (init) = false;
1727 TREE_SIDE_EFFECTS (init) = true;
1729 else if (flags & PICFLAG_NOT_ALL_CONSTANT)
1730 /* Make sure TREE_CONSTANT isn't set from build_constructor. */
1731 TREE_CONSTANT (init) = false;
1732 else
1734 TREE_CONSTANT (init) = 1;
1735 if (!(flags & PICFLAG_NOT_ALL_SIMPLE))
1736 TREE_STATIC (init) = 1;
1738 return init;
1741 /* Given a structure or union value DATUM, construct and return
1742 the structure or union component which results from narrowing
1743 that value to the base specified in BASETYPE. For example, given the
1744 hierarchy
1746 class L { int ii; };
1747 class A : L { ... };
1748 class B : L { ... };
1749 class C : A, B { ... };
1751 and the declaration
1753 C x;
1755 then the expression
1757 x.A::ii refers to the ii member of the L part of
1758 the A part of the C object named by X. In this case,
1759 DATUM would be x, and BASETYPE would be A.
1761 I used to think that this was nonconformant, that the standard specified
1762 that first we look up ii in A, then convert x to an L& and pull out the
1763 ii part. But in fact, it does say that we convert x to an A&; A here
1764 is known as the "naming class". (jason 2000-12-19)
1766 BINFO_P points to a variable initialized either to NULL_TREE or to the
1767 binfo for the specific base subobject we want to convert to. */
1769 tree
1770 build_scoped_ref (tree datum, tree basetype, tree* binfo_p)
1772 tree binfo;
1774 if (datum == error_mark_node)
1775 return error_mark_node;
1776 if (*binfo_p)
1777 binfo = *binfo_p;
1778 else
1779 binfo = lookup_base (TREE_TYPE (datum), basetype, ba_check,
1780 NULL, tf_warning_or_error);
1782 if (!binfo || binfo == error_mark_node)
1784 *binfo_p = NULL_TREE;
1785 if (!binfo)
1786 error_not_base_type (basetype, TREE_TYPE (datum));
1787 return error_mark_node;
1790 *binfo_p = binfo;
1791 return build_base_path (PLUS_EXPR, datum, binfo, 1,
1792 tf_warning_or_error);
1795 /* Build a reference to an object specified by the C++ `->' operator.
1796 Usually this just involves dereferencing the object, but if the
1797 `->' operator is overloaded, then such overloads must be
1798 performed until an object which does not have the `->' operator
1799 overloaded is found. An error is reported when circular pointer
1800 delegation is detected. */
1802 tree
1803 build_x_arrow (location_t loc, tree expr, tsubst_flags_t complain)
1805 tree orig_expr = expr;
1806 tree type = TREE_TYPE (expr);
1807 tree last_rval = NULL_TREE;
1808 vec<tree, va_gc> *types_memoized = NULL;
1810 if (type == error_mark_node)
1811 return error_mark_node;
1813 if (processing_template_decl)
1815 if (type && TREE_CODE (type) == POINTER_TYPE
1816 && !dependent_scope_p (TREE_TYPE (type)))
1817 /* Pointer to current instantiation, don't treat as dependent. */;
1818 else if (type_dependent_expression_p (expr))
1819 return build_min_nt_loc (loc, ARROW_EXPR, expr);
1820 expr = build_non_dependent_expr (expr);
1823 if (MAYBE_CLASS_TYPE_P (type))
1825 struct tinst_level *actual_inst = current_instantiation ();
1826 tree fn = NULL;
1828 while ((expr = build_new_op (loc, COMPONENT_REF,
1829 LOOKUP_NORMAL, expr, NULL_TREE, NULL_TREE,
1830 &fn, complain)))
1832 if (expr == error_mark_node)
1833 return error_mark_node;
1835 /* This provides a better instantiation backtrace in case of
1836 error. */
1837 if (fn && DECL_USE_TEMPLATE (fn))
1838 push_tinst_level_loc (fn,
1839 (current_instantiation () != actual_inst)
1840 ? DECL_SOURCE_LOCATION (fn)
1841 : input_location);
1842 fn = NULL;
1844 if (vec_member (TREE_TYPE (expr), types_memoized))
1846 if (complain & tf_error)
1847 error ("circular pointer delegation detected");
1848 return error_mark_node;
1851 vec_safe_push (types_memoized, TREE_TYPE (expr));
1852 last_rval = expr;
1855 while (current_instantiation () != actual_inst)
1856 pop_tinst_level ();
1858 if (last_rval == NULL_TREE)
1860 if (complain & tf_error)
1861 error ("base operand of %<->%> has non-pointer type %qT", type);
1862 return error_mark_node;
1865 if (TREE_CODE (TREE_TYPE (last_rval)) == REFERENCE_TYPE)
1866 last_rval = convert_from_reference (last_rval);
1868 else
1869 last_rval = decay_conversion (expr, complain);
1871 if (TYPE_PTR_P (TREE_TYPE (last_rval)))
1873 if (processing_template_decl)
1875 expr = build_min (ARROW_EXPR, TREE_TYPE (TREE_TYPE (last_rval)),
1876 orig_expr);
1877 TREE_SIDE_EFFECTS (expr) = TREE_SIDE_EFFECTS (last_rval);
1878 return expr;
1881 return cp_build_indirect_ref (last_rval, RO_ARROW, complain);
1884 if (complain & tf_error)
1886 if (types_memoized)
1887 error ("result of %<operator->()%> yields non-pointer result");
1888 else
1889 error ("base operand of %<->%> is not a pointer");
1891 return error_mark_node;
1894 /* Return an expression for "DATUM .* COMPONENT". DATUM has not
1895 already been checked out to be of aggregate type. */
1897 tree
1898 build_m_component_ref (tree datum, tree component, tsubst_flags_t complain)
1900 tree ptrmem_type;
1901 tree objtype;
1902 tree type;
1903 tree binfo;
1904 tree ctype;
1906 if (error_operand_p (datum) || error_operand_p (component))
1907 return error_mark_node;
1909 datum = mark_lvalue_use (datum);
1910 component = mark_rvalue_use (component);
1912 ptrmem_type = TREE_TYPE (component);
1913 if (!TYPE_PTRMEM_P (ptrmem_type))
1915 if (complain & tf_error)
1916 error ("%qE cannot be used as a member pointer, since it is of "
1917 "type %qT", component, ptrmem_type);
1918 return error_mark_node;
1921 objtype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
1922 if (! MAYBE_CLASS_TYPE_P (objtype))
1924 if (complain & tf_error)
1925 error ("cannot apply member pointer %qE to %qE, which is of "
1926 "non-class type %qT", component, datum, objtype);
1927 return error_mark_node;
1930 type = TYPE_PTRMEM_POINTED_TO_TYPE (ptrmem_type);
1931 ctype = complete_type (TYPE_PTRMEM_CLASS_TYPE (ptrmem_type));
1933 if (!COMPLETE_TYPE_P (ctype))
1935 if (!same_type_p (ctype, objtype))
1936 goto mismatch;
1937 binfo = NULL;
1939 else
1941 binfo = lookup_base (objtype, ctype, ba_check, NULL, complain);
1943 if (!binfo)
1945 mismatch:
1946 if (complain & tf_error)
1947 error ("pointer to member type %qT incompatible with object "
1948 "type %qT", type, objtype);
1949 return error_mark_node;
1951 else if (binfo == error_mark_node)
1952 return error_mark_node;
1955 if (TYPE_PTRDATAMEM_P (ptrmem_type))
1957 cp_lvalue_kind kind = lvalue_kind (datum);
1958 tree ptype;
1960 /* Compute the type of the field, as described in [expr.ref].
1961 There's no such thing as a mutable pointer-to-member, so
1962 things are not as complex as they are for references to
1963 non-static data members. */
1964 type = cp_build_qualified_type (type,
1965 (cp_type_quals (type)
1966 | cp_type_quals (TREE_TYPE (datum))));
1968 datum = build_address (datum);
1970 /* Convert object to the correct base. */
1971 if (binfo)
1973 datum = build_base_path (PLUS_EXPR, datum, binfo, 1, complain);
1974 if (datum == error_mark_node)
1975 return error_mark_node;
1978 /* Build an expression for "object + offset" where offset is the
1979 value stored in the pointer-to-data-member. */
1980 ptype = build_pointer_type (type);
1981 datum = fold_build_pointer_plus (fold_convert (ptype, datum), component);
1982 datum = cp_build_fold_indirect_ref (datum);
1983 if (datum == error_mark_node)
1984 return error_mark_node;
1986 /* If the object expression was an rvalue, return an rvalue. */
1987 if (kind & clk_class)
1988 datum = rvalue (datum);
1989 else if (kind & clk_rvalueref)
1990 datum = move (datum);
1991 return datum;
1993 else
1995 /* 5.5/6: In a .* expression whose object expression is an rvalue, the
1996 program is ill-formed if the second operand is a pointer to member
1997 function with ref-qualifier & (for C++2A: unless its cv-qualifier-seq
1998 is const). In a .* expression whose object expression is an lvalue,
1999 the program is ill-formed if the second operand is a pointer to member
2000 function with ref-qualifier &&. */
2001 if (FUNCTION_REF_QUALIFIED (type))
2003 bool lval = lvalue_p (datum);
2004 if (lval && FUNCTION_RVALUE_QUALIFIED (type))
2006 if (complain & tf_error)
2007 error ("pointer-to-member-function type %qT requires an rvalue",
2008 ptrmem_type);
2009 return error_mark_node;
2011 else if (!lval && !FUNCTION_RVALUE_QUALIFIED (type))
2013 if ((type_memfn_quals (type)
2014 & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE))
2015 != TYPE_QUAL_CONST)
2017 if (complain & tf_error)
2018 error ("pointer-to-member-function type %qT requires "
2019 "an lvalue", ptrmem_type);
2020 return error_mark_node;
2022 else if (cxx_dialect < cxx2a)
2024 if (complain & tf_warning_or_error)
2025 pedwarn (input_location, OPT_Wpedantic,
2026 "pointer-to-member-function type %qT requires "
2027 "an lvalue before C++2a", ptrmem_type);
2028 else
2029 return error_mark_node;
2033 return build2 (OFFSET_REF, type, datum, component);
2037 /* Return a tree node for the expression TYPENAME '(' PARMS ')'. */
2039 tree
2040 build_functional_cast (tree exp, tree parms, tsubst_flags_t complain)
2042 /* This is either a call to a constructor,
2043 or a C cast in C++'s `functional' notation. */
2045 /* The type to which we are casting. */
2046 tree type;
2047 vec<tree, va_gc> *parmvec;
2049 if (error_operand_p (exp) || parms == error_mark_node)
2050 return error_mark_node;
2052 if (TREE_CODE (exp) == TYPE_DECL)
2054 type = TREE_TYPE (exp);
2056 if (complain & tf_warning
2057 && TREE_DEPRECATED (type)
2058 && DECL_ARTIFICIAL (exp))
2059 warn_deprecated_use (type, NULL_TREE);
2061 else
2062 type = exp;
2064 /* We need to check this explicitly, since value-initialization of
2065 arrays is allowed in other situations. */
2066 if (TREE_CODE (type) == ARRAY_TYPE)
2068 if (complain & tf_error)
2069 error ("functional cast to array type %qT", type);
2070 return error_mark_node;
2073 if (tree anode = type_uses_auto (type))
2075 if (!CLASS_PLACEHOLDER_TEMPLATE (anode))
2077 if (complain & tf_error)
2078 error ("invalid use of %qT", anode);
2079 return error_mark_node;
2081 else if (!parms)
2083 if (complain & tf_error)
2084 error ("cannot deduce template arguments for %qT from ()", anode);
2085 return error_mark_node;
2087 else
2088 type = do_auto_deduction (type, parms, anode, complain,
2089 adc_variable_type);
2092 if (processing_template_decl)
2094 tree t;
2096 /* Diagnose this even in a template. We could also try harder
2097 to give all the usual errors when the type and args are
2098 non-dependent... */
2099 if (TREE_CODE (type) == REFERENCE_TYPE && !parms)
2101 if (complain & tf_error)
2102 error ("invalid value-initialization of reference type");
2103 return error_mark_node;
2106 t = build_min (CAST_EXPR, type, parms);
2107 /* We don't know if it will or will not have side effects. */
2108 TREE_SIDE_EFFECTS (t) = 1;
2109 return t;
2112 if (! MAYBE_CLASS_TYPE_P (type))
2114 if (parms == NULL_TREE)
2116 if (VOID_TYPE_P (type))
2117 return void_node;
2118 return build_value_init (cv_unqualified (type), complain);
2121 /* This must build a C cast. */
2122 parms = build_x_compound_expr_from_list (parms, ELK_FUNC_CAST, complain);
2123 return cp_build_c_cast (type, parms, complain);
2126 /* Prepare to evaluate as a call to a constructor. If this expression
2127 is actually used, for example,
2129 return X (arg1, arg2, ...);
2131 then the slot being initialized will be filled in. */
2133 if (!complete_type_or_maybe_complain (type, NULL_TREE, complain))
2134 return error_mark_node;
2135 if (abstract_virtuals_error_sfinae (ACU_CAST, type, complain))
2136 return error_mark_node;
2138 /* [expr.type.conv]
2140 If the expression list is a single-expression, the type
2141 conversion is equivalent (in definedness, and if defined in
2142 meaning) to the corresponding cast expression. */
2143 if (parms && TREE_CHAIN (parms) == NULL_TREE)
2144 return cp_build_c_cast (type, TREE_VALUE (parms), complain);
2146 /* [expr.type.conv]
2148 The expression T(), where T is a simple-type-specifier for a
2149 non-array complete object type or the (possibly cv-qualified)
2150 void type, creates an rvalue of the specified type, which is
2151 value-initialized. */
2153 if (parms == NULL_TREE)
2155 exp = build_value_init (type, complain);
2156 exp = get_target_expr_sfinae (exp, complain);
2157 return exp;
2160 /* Call the constructor. */
2161 parmvec = make_tree_vector ();
2162 for (; parms != NULL_TREE; parms = TREE_CHAIN (parms))
2163 vec_safe_push (parmvec, TREE_VALUE (parms));
2164 exp = build_special_member_call (NULL_TREE, complete_ctor_identifier,
2165 &parmvec, type, LOOKUP_NORMAL, complain);
2166 release_tree_vector (parmvec);
2168 if (exp == error_mark_node)
2169 return error_mark_node;
2171 return build_cplus_new (type, exp, complain);
2175 /* Add new exception specifier SPEC, to the LIST we currently have.
2176 If it's already in LIST then do nothing.
2177 Moan if it's bad and we're allowed to. COMPLAIN < 0 means we
2178 know what we're doing. */
2180 tree
2181 add_exception_specifier (tree list, tree spec, int complain)
2183 bool ok;
2184 tree core = spec;
2185 bool is_ptr;
2186 diagnostic_t diag_type = DK_UNSPECIFIED; /* none */
2188 if (spec == error_mark_node)
2189 return list;
2191 gcc_assert (spec && (!list || TREE_VALUE (list)));
2193 /* [except.spec] 1, type in an exception specifier shall not be
2194 incomplete, or pointer or ref to incomplete other than pointer
2195 to cv void. */
2196 is_ptr = TYPE_PTR_P (core);
2197 if (is_ptr || TREE_CODE (core) == REFERENCE_TYPE)
2198 core = TREE_TYPE (core);
2199 if (complain < 0)
2200 ok = true;
2201 else if (VOID_TYPE_P (core))
2202 ok = is_ptr;
2203 else if (TREE_CODE (core) == TEMPLATE_TYPE_PARM)
2204 ok = true;
2205 else if (processing_template_decl)
2206 ok = true;
2207 else
2209 ok = true;
2210 /* 15.4/1 says that types in an exception specifier must be complete,
2211 but it seems more reasonable to only require this on definitions
2212 and calls. So just give a pedwarn at this point; we will give an
2213 error later if we hit one of those two cases. */
2214 if (!COMPLETE_TYPE_P (complete_type (core)))
2215 diag_type = DK_PEDWARN; /* pedwarn */
2218 if (ok)
2220 tree probe;
2222 for (probe = list; probe; probe = TREE_CHAIN (probe))
2223 if (same_type_p (TREE_VALUE (probe), spec))
2224 break;
2225 if (!probe)
2226 list = tree_cons (NULL_TREE, spec, list);
2228 else
2229 diag_type = DK_ERROR; /* error */
2231 if (diag_type != DK_UNSPECIFIED
2232 && (complain & tf_warning_or_error))
2233 cxx_incomplete_type_diagnostic (NULL_TREE, core, diag_type);
2235 return list;
2238 /* Like nothrow_spec_p, but don't abort on deferred noexcept. */
2240 static bool
2241 nothrow_spec_p_uninst (const_tree spec)
2243 if (DEFERRED_NOEXCEPT_SPEC_P (spec))
2244 return false;
2245 return nothrow_spec_p (spec);
2248 /* Combine the two exceptions specifier lists LIST and ADD, and return
2249 their union. */
2251 tree
2252 merge_exception_specifiers (tree list, tree add)
2254 tree noex, orig_list;
2256 /* No exception-specifier or noexcept(false) are less strict than
2257 anything else. Prefer the newer variant (LIST). */
2258 if (!list || list == noexcept_false_spec)
2259 return list;
2260 else if (!add || add == noexcept_false_spec)
2261 return add;
2263 /* noexcept(true) and throw() are stricter than anything else.
2264 As above, prefer the more recent one (LIST). */
2265 if (nothrow_spec_p_uninst (add))
2266 return list;
2268 /* Two implicit noexcept specs (e.g. on a destructor) are equivalent. */
2269 if (UNEVALUATED_NOEXCEPT_SPEC_P (add)
2270 && UNEVALUATED_NOEXCEPT_SPEC_P (list))
2271 return list;
2272 /* We should have instantiated other deferred noexcept specs by now. */
2273 gcc_assert (!DEFERRED_NOEXCEPT_SPEC_P (add));
2275 if (nothrow_spec_p_uninst (list))
2276 return add;
2277 noex = TREE_PURPOSE (list);
2278 gcc_checking_assert (!TREE_PURPOSE (add)
2279 || errorcount || !flag_exceptions
2280 || cp_tree_equal (noex, TREE_PURPOSE (add)));
2282 /* Combine the dynamic-exception-specifiers, if any. */
2283 orig_list = list;
2284 for (; add && TREE_VALUE (add); add = TREE_CHAIN (add))
2286 tree spec = TREE_VALUE (add);
2287 tree probe;
2289 for (probe = orig_list; probe && TREE_VALUE (probe);
2290 probe = TREE_CHAIN (probe))
2291 if (same_type_p (TREE_VALUE (probe), spec))
2292 break;
2293 if (!probe)
2295 spec = build_tree_list (NULL_TREE, spec);
2296 TREE_CHAIN (spec) = list;
2297 list = spec;
2301 /* Keep the noexcept-specifier at the beginning of the list. */
2302 if (noex != TREE_PURPOSE (list))
2303 list = tree_cons (noex, TREE_VALUE (list), TREE_CHAIN (list));
2305 return list;
2308 /* Subroutine of build_call. Ensure that each of the types in the
2309 exception specification is complete. Technically, 15.4/1 says that
2310 they need to be complete when we see a declaration of the function,
2311 but we should be able to get away with only requiring this when the
2312 function is defined or called. See also add_exception_specifier. */
2314 void
2315 require_complete_eh_spec_types (tree fntype, tree decl)
2317 tree raises;
2318 /* Don't complain about calls to op new. */
2319 if (decl && DECL_ARTIFICIAL (decl))
2320 return;
2321 for (raises = TYPE_RAISES_EXCEPTIONS (fntype); raises;
2322 raises = TREE_CHAIN (raises))
2324 tree type = TREE_VALUE (raises);
2325 if (type && !COMPLETE_TYPE_P (type))
2327 if (decl)
2328 error
2329 ("call to function %qD which throws incomplete type %q#T",
2330 decl, type);
2331 else
2332 error ("call to function which throws incomplete type %q#T",
2333 decl);
2339 #include "gt-cp-typeck2.h"