1 /* Report error messages, build initializers, and perform
2 some front-end optimizations for C++ compiler.
3 Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
4 1999, 2000, 2001, 2002, 2004, 2005, 2006, 2007, 2008, 2009, 2010
5 Free Software Foundation, Inc.
6 Hacked by Michael Tiemann (tiemann@cygnus.com)
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3, or (at your option)
15 GCC is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3. If not see
22 <http://www.gnu.org/licenses/>. */
25 /* This file is part of the C++ front end.
26 It contains routines to build C++ expressions given their operands,
27 including computing the types of the result, C and C++ specific error
28 checks, and some optimization. */
32 #include "coretypes.h"
39 #include "diagnostic-core.h"
42 process_init_constructor (tree type, tree init);
45 /* Print an error message stemming from an attempt to use
46 BASETYPE as a base class for TYPE. */
49 error_not_base_type (tree basetype, tree type)
51 if (TREE_CODE (basetype) == FUNCTION_DECL)
52 basetype = DECL_CONTEXT (basetype);
53 error ("type %qT is not a base type for type %qT", basetype, type);
54 return error_mark_node;
58 binfo_or_else (tree base, tree type)
60 tree binfo = lookup_base (type, base, ba_unique, NULL);
62 if (binfo == error_mark_node)
65 error_not_base_type (base, type);
69 /* According to ARM $7.1.6, "A `const' object may be initialized, but its
70 value may not be changed thereafter. */
73 cxx_readonly_error (tree arg, enum lvalue_use errstring)
76 /* This macro is used to emit diagnostics to ensure that all format
77 strings are complete sentences, visible to gettext and checked at
80 #define ERROR_FOR_ASSIGNMENT(AS, ASM, IN, DE, ARG) \
101 /* Handle C++-specific things first. */
103 if (TREE_CODE (arg) == VAR_DECL
104 && DECL_LANG_SPECIFIC (arg)
105 && DECL_IN_AGGR_P (arg)
106 && !TREE_STATIC (arg))
107 ERROR_FOR_ASSIGNMENT (G_("assignment of "
108 "constant field %qD"),
109 G_("constant field %qD "
110 "used as %<asm%> output"),
112 "constant field %qD"),
114 "constant field %qD"),
116 else if (TREE_CODE (arg) == INDIRECT_REF
117 && TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 0))) == REFERENCE_TYPE
118 && (TREE_CODE (TREE_OPERAND (arg, 0)) == VAR_DECL
119 || TREE_CODE (TREE_OPERAND (arg, 0)) == PARM_DECL))
120 ERROR_FOR_ASSIGNMENT (G_("assignment of "
121 "read-only reference %qD"),
122 G_("read-only reference %qD "
123 "used as %<asm%> output"),
125 "read-only reference %qD"),
127 "read-only reference %qD"),
128 TREE_OPERAND (arg, 0));
130 readonly_error (arg, errstring);
134 /* Structure that holds information about declarations whose type was
135 incomplete and we could not check whether it was abstract or not. */
137 struct GTY((chain_next ("%h.next"))) pending_abstract_type {
138 /* Declaration which we are checking for abstractness. It is either
139 a DECL node, or an IDENTIFIER_NODE if we do not have a full
140 declaration available. */
143 /* Type which will be checked for abstractness. */
146 /* Position of the declaration. This is only needed for IDENTIFIER_NODEs,
147 because DECLs already carry locus information. */
150 /* Link to the next element in list. */
151 struct pending_abstract_type* next;
155 /* Compute the hash value of the node VAL. This function is used by the
156 hash table abstract_pending_vars. */
159 pat_calc_hash (const void* val)
161 const struct pending_abstract_type *pat =
162 (const struct pending_abstract_type *) val;
163 return (hashval_t) TYPE_UID (pat->type);
167 /* Compare node VAL1 with the type VAL2. This function is used by the
168 hash table abstract_pending_vars. */
171 pat_compare (const void* val1, const void* val2)
173 const struct pending_abstract_type *const pat1 =
174 (const struct pending_abstract_type *) val1;
175 const_tree const type2 = (const_tree)val2;
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 ((param_is (struct pending_abstract_type)))
184 htab_t abstract_pending_vars = NULL;
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. */
193 complete_type_check_abstract (tree type)
196 struct pending_abstract_type *pat;
197 location_t cur_loc = input_location;
199 gcc_assert (COMPLETE_TYPE_P (type));
201 if (!abstract_pending_vars)
204 /* Retrieve the list of pending declarations for this type. */
205 slot = htab_find_slot_with_hash (abstract_pending_vars, type,
206 (hashval_t)TYPE_UID (type), NO_INSERT);
209 pat = (struct pending_abstract_type*)*slot;
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)
226 /* Go through the list, and call abstract_virtuals_error for each
227 element: it will issue a diagnostic if the type is abstract. */
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
235 input_location = pat->locus;
236 abstract_virtuals_error (pat->decl, pat->type);
241 htab_clear_slot (abstract_pending_vars, slot);
243 input_location = cur_loc;
247 /* If TYPE has abstract virtual functions, issue an error about trying
248 to create an object of that type. DECL is the object declared, or
249 NULL_TREE if the declaration is unavailable. Returns 1 if an error
250 occurred; zero if all was well. */
253 abstract_virtuals_error_sfinae (tree decl, tree type, tsubst_flags_t complain)
257 /* This function applies only to classes. Any other entity can never
259 if (!CLASS_TYPE_P (type))
261 type = TYPE_MAIN_VARIANT (type);
263 /* If the type is incomplete, we register it within a hash table,
264 so that we can check again once it is completed. This makes sense
265 only for objects for which we have a declaration or at least a
267 if (!COMPLETE_TYPE_P (type))
270 struct pending_abstract_type *pat;
272 gcc_assert (!decl || DECL_P (decl)
273 || TREE_CODE (decl) == IDENTIFIER_NODE);
275 if (!abstract_pending_vars)
276 abstract_pending_vars = htab_create_ggc (31, &pat_calc_hash,
279 slot = htab_find_slot_with_hash (abstract_pending_vars, type,
280 (hashval_t)TYPE_UID (type), INSERT);
282 pat = ggc_alloc_pending_abstract_type ();
285 pat->locus = ((decl && DECL_P (decl))
286 ? DECL_SOURCE_LOCATION (decl)
289 pat->next = (struct pending_abstract_type *) *slot;
295 if (!TYPE_SIZE (type))
296 /* TYPE is being defined, and during that time
297 CLASSTYPE_PURE_VIRTUALS holds the inline friends. */
300 pure = CLASSTYPE_PURE_VIRTUALS (type);
304 if (!(complain & tf_error))
309 if (TREE_CODE (decl) == VAR_DECL)
310 error ("cannot declare variable %q+D to be of abstract "
311 "type %qT", decl, type);
312 else if (TREE_CODE (decl) == PARM_DECL)
313 error ("cannot declare parameter %q+D to be of abstract type %qT",
315 else if (TREE_CODE (decl) == FIELD_DECL)
316 error ("cannot declare field %q+D to be of abstract type %qT",
318 else if (TREE_CODE (decl) == FUNCTION_DECL
319 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
320 error ("invalid abstract return type for member function %q+#D", decl);
321 else if (TREE_CODE (decl) == FUNCTION_DECL)
322 error ("invalid abstract return type for function %q+#D", decl);
323 else if (TREE_CODE (decl) == IDENTIFIER_NODE)
324 /* Here we do not have location information. */
325 error ("invalid abstract type %qT for %qE", type, decl);
327 error ("invalid abstract type for %q+D", decl);
330 error ("cannot allocate an object of abstract type %qT", type);
332 /* Only go through this once. */
333 if (VEC_length (tree, pure))
338 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
339 " because the following virtual functions are pure within %qT:",
342 FOR_EACH_VEC_ELT (tree, pure, ix, fn)
343 inform (input_location, "\t%+#D", fn);
344 /* Now truncate the vector. This leaves it non-null, so we know
345 there are pure virtuals, but empty so we don't list them out
347 VEC_truncate (tree, pure, 0);
350 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
351 " since type %qT has pure virtual functions",
357 /* Wrapper for the above function in the common case of wanting errors. */
360 abstract_virtuals_error (tree decl, tree type)
362 return abstract_virtuals_error_sfinae (decl, type, tf_warning_or_error);
365 /* Print an error message for invalid use of an incomplete type.
366 VALUE is the expression that was used (or 0 if that isn't known)
367 and TYPE is the type that was invalid. DIAG_KIND indicates the
368 type of diagnostic (see diagnostic.def). */
371 cxx_incomplete_type_diagnostic (const_tree value, const_tree type,
372 diagnostic_t diag_kind)
376 gcc_assert (diag_kind == DK_WARNING
377 || diag_kind == DK_PEDWARN
378 || diag_kind == DK_ERROR);
380 /* Avoid duplicate error message. */
381 if (TREE_CODE (type) == ERROR_MARK)
384 if (value != 0 && (TREE_CODE (value) == VAR_DECL
385 || TREE_CODE (value) == PARM_DECL
386 || TREE_CODE (value) == FIELD_DECL))
388 emit_diagnostic (diag_kind, input_location, 0,
389 "%q+D has incomplete type", value);
393 /* We must print an error message. Be clever about what it says. */
395 switch (TREE_CODE (type))
401 emit_diagnostic (diag_kind, input_location, 0,
402 "invalid use of incomplete type %q#T", type);
403 if (!TYPE_TEMPLATE_INFO (type))
404 emit_diagnostic (diag_kind, input_location, 0,
405 "forward declaration of %q+#T", type);
407 emit_diagnostic (diag_kind, input_location, 0,
408 "declaration of %q+#T", type);
412 emit_diagnostic (diag_kind, input_location, 0,
413 "invalid use of %qT", type);
417 if (TYPE_DOMAIN (type))
419 type = TREE_TYPE (type);
422 emit_diagnostic (diag_kind, input_location, 0,
423 "invalid use of array with unspecified bounds");
428 emit_diagnostic (diag_kind, input_location, 0,
429 "invalid use of member (did you forget the %<&%> ?)");
432 case TEMPLATE_TYPE_PARM:
434 emit_diagnostic (diag_kind, input_location, 0,
435 "invalid use of %<auto%>");
437 emit_diagnostic (diag_kind, input_location, 0,
438 "invalid use of template type parameter %qT", type);
441 case BOUND_TEMPLATE_TEMPLATE_PARM:
442 emit_diagnostic (diag_kind, input_location, 0,
443 "invalid use of template template parameter %qT",
448 emit_diagnostic (diag_kind, input_location, 0,
449 "invalid use of dependent type %qT", type);
453 gcc_assert (type == unknown_type_node);
454 if (value && TREE_CODE (value) == COMPONENT_REF)
456 else if (value && TREE_CODE (value) == ADDR_EXPR)
457 emit_diagnostic (diag_kind, input_location, 0,
458 "address of overloaded function with no contextual "
460 else if (value && TREE_CODE (value) == OVERLOAD)
461 emit_diagnostic (diag_kind, input_location, 0,
462 "overloaded function with no contextual type information");
464 emit_diagnostic (diag_kind, input_location, 0,
465 "insufficient contextual information to determine type");
473 /* Backward-compatibility interface to incomplete_type_diagnostic;
474 required by ../tree.c. */
475 #undef cxx_incomplete_type_error
477 cxx_incomplete_type_error (const_tree value, const_tree type)
479 cxx_incomplete_type_diagnostic (value, type, DK_ERROR);
483 /* The recursive part of split_nonconstant_init. DEST is an lvalue
484 expression to which INIT should be assigned. INIT is a CONSTRUCTOR. */
487 split_nonconstant_init_1 (tree dest, tree *initp)
489 unsigned HOST_WIDE_INT idx;
491 tree field_index, value;
492 tree type = TREE_TYPE (dest);
493 tree inner_type = NULL;
494 bool array_type_p = false;
495 HOST_WIDE_INT num_type_elements, num_initialized_elements;
497 switch (TREE_CODE (type))
500 inner_type = TREE_TYPE (type);
506 case QUAL_UNION_TYPE:
507 num_initialized_elements = 0;
508 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), idx,
511 /* The current implementation of this algorithm assumes that
512 the field was set for all the elements. This is usually done
513 by process_init_constructor. */
514 gcc_assert (field_index);
517 inner_type = TREE_TYPE (field_index);
519 if (TREE_CODE (value) == CONSTRUCTOR)
524 sub = build4 (ARRAY_REF, inner_type, dest, field_index,
525 NULL_TREE, NULL_TREE);
527 sub = build3 (COMPONENT_REF, inner_type, dest, field_index,
530 split_nonconstant_init_1 (sub, &value);
532 else if (!initializer_constant_valid_p (value, inner_type))
536 HOST_WIDE_INT inner_elements;
538 /* FIXME: Ordered removal is O(1) so the whole function is
539 worst-case quadratic. This could be fixed using an aside
540 bitmap to record which elements must be removed and remove
541 them all at the same time. Or by merging
542 split_non_constant_init into process_init_constructor_array,
543 that is separating constants from non-constants while building
545 VEC_ordered_remove (constructor_elt, CONSTRUCTOR_ELTS (init),
550 sub = build4 (ARRAY_REF, inner_type, dest, field_index,
551 NULL_TREE, NULL_TREE);
553 sub = build3 (COMPONENT_REF, inner_type, dest, field_index,
556 code = build2 (INIT_EXPR, inner_type, sub, value);
557 code = build_stmt (input_location, EXPR_STMT, code);
560 inner_elements = count_type_elements (inner_type, true);
561 if (inner_elements < 0)
562 num_initialized_elements = -1;
563 else if (num_initialized_elements >= 0)
564 num_initialized_elements += inner_elements;
569 num_type_elements = count_type_elements (type, true);
570 /* If all elements of the initializer are non-constant and
571 have been split out, we don't need the empty CONSTRUCTOR. */
572 if (num_type_elements > 0
573 && num_type_elements == num_initialized_elements)
578 if (!initializer_constant_valid_p (init, type))
581 tree cons = copy_node (init);
582 CONSTRUCTOR_ELTS (init) = NULL;
583 code = build2 (MODIFY_EXPR, type, dest, cons);
584 code = build_stmt (input_location, EXPR_STMT, code);
593 /* The rest of the initializer is now a constant. */
594 TREE_CONSTANT (init) = 1;
597 /* A subroutine of store_init_value. Splits non-constant static
598 initializer INIT into a constant part and generates code to
599 perform the non-constant part of the initialization to DEST.
600 Returns the code for the runtime init. */
603 split_nonconstant_init (tree dest, tree init)
607 if (TREE_CODE (init) == CONSTRUCTOR)
609 code = push_stmt_list ();
610 split_nonconstant_init_1 (dest, &init);
611 code = pop_stmt_list (code);
612 DECL_INITIAL (dest) = init;
613 TREE_READONLY (dest) = 0;
616 code = build2 (INIT_EXPR, TREE_TYPE (dest), dest, init);
621 /* Perform appropriate conversions on the initial value of a variable,
622 store it in the declaration DECL,
623 and print any error messages that are appropriate.
624 If the init is invalid, store an ERROR_MARK.
626 C++: Note that INIT might be a TREE_LIST, which would mean that it is
627 a base class initializer for some aggregate type, hopefully compatible
628 with DECL. If INIT is a single element, and DECL is an aggregate
629 type, we silently convert INIT into a TREE_LIST, allowing a constructor
632 If INIT is a TREE_LIST and there is no constructor, turn INIT
633 into a CONSTRUCTOR and use standard initialization techniques.
634 Perhaps a warning should be generated?
636 Returns code to be executed if initialization could not be performed
637 for static variable. In that case, caller must emit the code. */
640 store_init_value (tree decl, tree init, int flags)
644 /* If variable's type was invalidly declared, just ignore it. */
646 type = TREE_TYPE (decl);
647 if (TREE_CODE (type) == ERROR_MARK)
650 if (MAYBE_CLASS_TYPE_P (type))
652 if (TREE_CODE (init) == TREE_LIST)
654 error ("constructor syntax used, but no constructor declared "
655 "for type %qT", type);
656 init = build_constructor_from_list (init_list_type_node, nreverse (init));
659 else if (TREE_CODE (init) == TREE_LIST
660 && TREE_TYPE (init) != unknown_type_node)
662 gcc_assert (TREE_CODE (decl) != RESULT_DECL);
664 if (TREE_CODE (init) == TREE_LIST
665 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
667 error ("cannot initialize arrays using this syntax");
671 /* We get here with code like `int a (2);' */
672 init = build_x_compound_expr_from_list (init, ELK_INIT,
673 tf_warning_or_error);
676 /* End of special C++ code. */
678 if (flags & LOOKUP_ALREADY_DIGESTED)
681 /* Digest the specified initializer into an expression. */
682 value = digest_init_flags (type, init, flags);
684 /* In C++0x constant expression is a semantic, not syntactic, property.
685 In C++98, make sure that what we thought was a constant expression at
686 template definition time is still constant. */
687 if ((cxx_dialect >= cxx0x
688 || DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
689 && (decl_maybe_constant_var_p (decl)
690 || TREE_STATIC (decl)))
693 value = fold_non_dependent_expr (value);
694 value = maybe_constant_init (value);
695 if (DECL_DECLARED_CONSTEXPR_P (decl))
696 /* Diagnose a non-constant initializer for constexpr. */
697 value = cxx_constant_value (value);
698 const_init = (reduced_constant_expression_p (value)
699 || error_operand_p (value));
700 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = const_init;
701 TREE_CONSTANT (decl) = const_init && decl_maybe_constant_var_p (decl);
704 /* If the initializer is not a constant, fill in DECL_INITIAL with
705 the bits that are constant, and then return an expression that
706 will perform the dynamic initialization. */
707 if (value != error_mark_node
708 && (TREE_SIDE_EFFECTS (value)
709 || ! initializer_constant_valid_p (value, TREE_TYPE (value))))
710 return split_nonconstant_init (decl, value);
711 /* If the value is a constant, just put it in DECL_INITIAL. If DECL
712 is an automatic variable, the middle end will turn this into a
713 dynamic initialization later. */
714 DECL_INITIAL (decl) = value;
719 /* Give errors about narrowing conversions within { }. */
722 check_narrowing (tree type, tree init)
724 tree ftype = unlowered_expr_type (init);
728 if (!ARITHMETIC_TYPE_P (type))
731 if (BRACE_ENCLOSED_INITIALIZER_P (init)
732 && TREE_CODE (type) == COMPLEX_TYPE)
734 tree elttype = TREE_TYPE (type);
735 check_narrowing (elttype, CONSTRUCTOR_ELT (init, 0)->value);
736 if (CONSTRUCTOR_NELTS (init) > 1)
737 check_narrowing (elttype, CONSTRUCTOR_ELT (init, 1)->value);
741 init = maybe_constant_value (init);
743 if (TREE_CODE (type) == INTEGER_TYPE
744 && TREE_CODE (ftype) == REAL_TYPE)
746 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype)
747 && CP_INTEGRAL_TYPE_P (type))
749 if (TYPE_PRECISION (type) < TYPE_PRECISION (ftype)
750 && (TREE_CODE (init) != INTEGER_CST
751 || !int_fits_type_p (init, type)))
754 else if (TREE_CODE (ftype) == REAL_TYPE
755 && TREE_CODE (type) == REAL_TYPE)
757 if (TYPE_PRECISION (type) < TYPE_PRECISION (ftype))
759 if (TREE_CODE (init) == REAL_CST)
761 /* Issue 703: Loss of precision is OK as long as the value is
762 within the representable range of the new type. */
764 d = TREE_REAL_CST (init);
765 real_convert (&r, TYPE_MODE (type), &d);
773 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype)
774 && TREE_CODE (type) == REAL_TYPE)
777 if (TREE_CODE (init) == INTEGER_CST)
779 d = real_value_from_int_cst (0, init);
780 if (exact_real_truncate (TYPE_MODE (type), &d))
786 permerror (input_location, "narrowing conversion of %qE from %qT to %qT inside { }",
790 /* Process the initializer INIT for a variable of type TYPE, emitting
791 diagnostics for invalid initializers and converting the initializer as
794 For aggregate types, it assumes that reshape_init has already run, thus the
795 initializer will have the right shape (brace elision has been undone).
797 NESTED is true iff we are being called for an element of a CONSTRUCTOR. */
800 digest_init_r (tree type, tree init, bool nested, int flags)
802 enum tree_code code = TREE_CODE (type);
804 if (error_operand_p (init))
805 return error_mark_node;
809 /* We must strip the outermost array type when completing the type,
810 because the its bounds might be incomplete at the moment. */
811 if (!complete_type_or_else (TREE_CODE (type) == ARRAY_TYPE
812 ? TREE_TYPE (type) : type, NULL_TREE))
813 return error_mark_node;
815 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue
816 (g++.old-deja/g++.law/casts2.C). */
817 if (TREE_CODE (init) == NON_LVALUE_EXPR)
818 init = TREE_OPERAND (init, 0);
820 /* Initialization of an array of chars from a string constant. The initializer
821 can be optionally enclosed in braces, but reshape_init has already removed
822 them if they were present. */
823 if (code == ARRAY_TYPE)
825 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
826 if (char_type_p (typ1)
828 && TREE_CODE (init) == STRING_CST)
830 tree char_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (init)));
832 if (TYPE_PRECISION (typ1) == BITS_PER_UNIT)
834 if (char_type != char_type_node)
836 error ("char-array initialized from wide string");
837 return error_mark_node;
842 if (char_type == char_type_node)
844 error ("int-array initialized from non-wide string");
845 return error_mark_node;
847 else if (char_type != typ1)
849 error ("int-array initialized from incompatible wide string");
850 return error_mark_node;
854 TREE_TYPE (init) = type;
855 if (TYPE_DOMAIN (type) != 0 && TREE_CONSTANT (TYPE_SIZE (type)))
857 int size = TREE_INT_CST_LOW (TYPE_SIZE (type));
858 size = (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
859 /* In C it is ok to subtract 1 from the length of the string
860 because it's ok to ignore the terminating null char that is
861 counted in the length of the constant, but in C++ this would
863 if (size < TREE_STRING_LENGTH (init))
864 permerror (input_location, "initializer-string for array of chars is too long");
870 /* Handle scalar types (including conversions) and references. */
871 if ((TREE_CODE (type) != COMPLEX_TYPE
872 || BRACE_ENCLOSED_INITIALIZER_P (init))
873 && (SCALAR_TYPE_P (type) || code == REFERENCE_TYPE))
877 if (cxx_dialect != cxx98 && nested)
878 check_narrowing (type, init);
879 init = convert_for_initialization (0, type, init, flags,
880 ICR_INIT, NULL_TREE, 0,
881 tf_warning_or_error);
884 /* Skip any conversions since we'll be outputting the underlying
886 while (CONVERT_EXPR_P (*exp)
887 || TREE_CODE (*exp) == NON_LVALUE_EXPR)
888 exp = &TREE_OPERAND (*exp, 0);
890 *exp = cplus_expand_constant (*exp);
895 /* Come here only for aggregates: records, arrays, unions, complex numbers
897 gcc_assert (TREE_CODE (type) == ARRAY_TYPE
898 || TREE_CODE (type) == VECTOR_TYPE
899 || TREE_CODE (type) == RECORD_TYPE
900 || TREE_CODE (type) == UNION_TYPE
901 || TREE_CODE (type) == COMPLEX_TYPE);
903 if (BRACE_ENCLOSED_INITIALIZER_P (init)
904 && !TYPE_NON_AGGREGATE_CLASS (type))
905 return process_init_constructor (type, init);
908 if (COMPOUND_LITERAL_P (init) && TREE_CODE (type) == ARRAY_TYPE)
910 error ("cannot initialize aggregate of type %qT with "
911 "a compound literal", type);
913 return error_mark_node;
916 if (TREE_CODE (type) == ARRAY_TYPE
917 && !BRACE_ENCLOSED_INITIALIZER_P (init))
919 /* Allow the result of build_array_copy and of
920 build_value_init_noctor. */
921 if ((TREE_CODE (init) == TARGET_EXPR
922 || TREE_CODE (init) == CONSTRUCTOR)
923 && (same_type_ignoring_top_level_qualifiers_p
924 (type, TREE_TYPE (init))))
927 error ("array must be initialized with a brace-enclosed"
929 return error_mark_node;
932 return convert_for_initialization (NULL_TREE, type, init,
934 ICR_INIT, NULL_TREE, 0,
935 tf_warning_or_error);
940 digest_init (tree type, tree init)
942 return digest_init_r (type, init, false, LOOKUP_IMPLICIT);
946 digest_init_flags (tree type, tree init, int flags)
948 return digest_init_r (type, init, false, flags);
951 /* Set of flags used within process_init_constructor to describe the
953 #define PICFLAG_ERRONEOUS 1
954 #define PICFLAG_NOT_ALL_CONSTANT 2
955 #define PICFLAG_NOT_ALL_SIMPLE 4
957 /* Given an initializer INIT, return the flag (PICFLAG_*) which better
961 picflag_from_initializer (tree init)
963 if (init == error_mark_node)
964 return PICFLAG_ERRONEOUS;
965 else if (!TREE_CONSTANT (init))
966 return PICFLAG_NOT_ALL_CONSTANT;
967 else if (!initializer_constant_valid_p (init, TREE_TYPE (init)))
968 return PICFLAG_NOT_ALL_SIMPLE;
972 /* Subroutine of process_init_constructor, which will process an initializer
973 INIT for an array or vector of type TYPE. Returns the flags (PICFLAG_*)
974 which describe the initializers. */
977 process_init_constructor_array (tree type, tree init)
979 unsigned HOST_WIDE_INT i, len = 0;
981 bool unbounded = false;
983 VEC(constructor_elt,gc) *v = CONSTRUCTOR_ELTS (init);
985 gcc_assert (TREE_CODE (type) == ARRAY_TYPE
986 || TREE_CODE (type) == VECTOR_TYPE);
988 if (TREE_CODE (type) == ARRAY_TYPE)
990 tree domain = TYPE_DOMAIN (type);
992 len = (TREE_INT_CST_LOW (TYPE_MAX_VALUE (domain))
993 - TREE_INT_CST_LOW (TYPE_MIN_VALUE (domain))
996 unbounded = true; /* Take as many as there are. */
999 /* Vectors are like simple fixed-size arrays. */
1000 len = TYPE_VECTOR_SUBPARTS (type);
1002 /* There must not be more initializers than needed. */
1003 if (!unbounded && VEC_length (constructor_elt, v) > len)
1004 error ("too many initializers for %qT", type);
1006 FOR_EACH_VEC_ELT (constructor_elt, v, i, ce)
1010 gcc_assert (TREE_CODE (ce->index) == INTEGER_CST);
1011 if (compare_tree_int (ce->index, i) != 0)
1013 ce->value = error_mark_node;
1014 sorry ("non-trivial designated initializers not supported");
1018 ce->index = size_int (i);
1019 gcc_assert (ce->value);
1020 ce->value = digest_init_r (TREE_TYPE (type), ce->value, true, LOOKUP_IMPLICIT);
1022 if (ce->value != error_mark_node)
1023 gcc_assert (same_type_ignoring_top_level_qualifiers_p
1024 (TREE_TYPE (type), TREE_TYPE (ce->value)));
1026 flags |= picflag_from_initializer (ce->value);
1029 /* No more initializers. If the array is unbounded, we are done. Otherwise,
1030 we must add initializers ourselves. */
1032 for (; i < len; ++i)
1036 if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (type)))
1038 /* If this type needs constructors run for default-initialization,
1039 we can't rely on the back end to do it for us, so build up
1040 TARGET_EXPRs. If the type in question is a class, just build
1041 one up; if it's an array, recurse. */
1042 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (type)))
1043 next = build_functional_cast (TREE_TYPE (type), NULL_TREE,
1044 tf_warning_or_error);
1046 next = build_constructor (init_list_type_node, NULL);
1047 next = digest_init (TREE_TYPE (type), next);
1049 else if (!zero_init_p (TREE_TYPE (type)))
1050 next = build_zero_init (TREE_TYPE (type),
1051 /*nelts=*/NULL_TREE,
1052 /*static_storage_p=*/false);
1054 /* The default zero-initialization is fine for us; don't
1055 add anything to the CONSTRUCTOR. */
1058 flags |= picflag_from_initializer (next);
1059 CONSTRUCTOR_APPEND_ELT (v, size_int (i), next);
1062 CONSTRUCTOR_ELTS (init) = v;
1066 /* Subroutine of process_init_constructor, which will process an initializer
1067 INIT for a class of type TYPE. Returns the flags (PICFLAG_*) which describe
1068 the initializers. */
1071 process_init_constructor_record (tree type, tree init)
1073 VEC(constructor_elt,gc) *v = NULL;
1076 unsigned HOST_WIDE_INT idx = 0;
1078 gcc_assert (TREE_CODE (type) == RECORD_TYPE);
1079 gcc_assert (!CLASSTYPE_VBASECLASSES (type));
1080 gcc_assert (!TYPE_BINFO (type)
1081 || !BINFO_N_BASE_BINFOS (TYPE_BINFO (type)));
1082 gcc_assert (!TYPE_POLYMORPHIC_P (type));
1084 /* Generally, we will always have an index for each initializer (which is
1085 a FIELD_DECL, put by reshape_init), but compound literals don't go trough
1086 reshape_init. So we need to handle both cases. */
1087 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
1092 if (!DECL_NAME (field) && DECL_C_BIT_FIELD (field))
1094 flags |= picflag_from_initializer (integer_zero_node);
1095 CONSTRUCTOR_APPEND_ELT (v, field, integer_zero_node);
1099 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
1102 /* If this is a bitfield, first convert to the declared type. */
1103 type = TREE_TYPE (field);
1104 if (DECL_BIT_FIELD_TYPE (field))
1105 type = DECL_BIT_FIELD_TYPE (field);
1107 if (idx < VEC_length (constructor_elt, CONSTRUCTOR_ELTS (init)))
1109 constructor_elt *ce = VEC_index (constructor_elt,
1110 CONSTRUCTOR_ELTS (init), idx);
1113 /* We can have either a FIELD_DECL or an IDENTIFIER_NODE. The
1114 latter case can happen in templates where lookup has to be
1116 gcc_assert (TREE_CODE (ce->index) == FIELD_DECL
1117 || TREE_CODE (ce->index) == IDENTIFIER_NODE);
1118 if (ce->index != field
1119 && ce->index != DECL_NAME (field))
1121 ce->value = error_mark_node;
1122 sorry ("non-trivial designated initializers not supported");
1126 gcc_assert (ce->value);
1127 next = digest_init_r (type, ce->value, true, LOOKUP_IMPLICIT);
1130 else if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (field)))
1132 /* If this type needs constructors run for
1133 default-initialization, we can't rely on the back end to do it
1134 for us, so build up TARGET_EXPRs. If the type in question is
1135 a class, just build one up; if it's an array, recurse. */
1136 next = build_constructor (init_list_type_node, NULL);
1137 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (field)))
1139 next = finish_compound_literal (TREE_TYPE (field), next,
1140 tf_warning_or_error);
1141 /* direct-initialize the target. No temporary is going
1143 if (TREE_CODE (next) == TARGET_EXPR)
1144 TARGET_EXPR_DIRECT_INIT_P (next) = true;
1147 next = digest_init_r (TREE_TYPE (field), next, true, LOOKUP_IMPLICIT);
1149 /* Warn when some struct elements are implicitly initialized. */
1150 warning (OPT_Wmissing_field_initializers,
1151 "missing initializer for member %qD", field);
1155 if (TREE_READONLY (field))
1156 error ("uninitialized const member %qD", field);
1157 else if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (TREE_TYPE (field)))
1158 error ("member %qD with uninitialized const fields", field);
1159 else if (TREE_CODE (TREE_TYPE (field)) == REFERENCE_TYPE)
1160 error ("member %qD is uninitialized reference", field);
1162 /* Warn when some struct elements are implicitly initialized
1164 warning (OPT_Wmissing_field_initializers,
1165 "missing initializer for member %qD", field);
1167 if (!zero_init_p (TREE_TYPE (field)))
1168 next = build_zero_init (TREE_TYPE (field), /*nelts=*/NULL_TREE,
1169 /*static_storage_p=*/false);
1171 /* The default zero-initialization is fine for us; don't
1172 add anything to the CONSTRUCTOR. */
1176 /* If this is a bitfield, now convert to the lowered type. */
1177 if (type != TREE_TYPE (field))
1178 next = cp_convert_and_check (TREE_TYPE (field), next);
1179 flags |= picflag_from_initializer (next);
1180 CONSTRUCTOR_APPEND_ELT (v, field, next);
1183 if (idx < VEC_length (constructor_elt, CONSTRUCTOR_ELTS (init)))
1184 error ("too many initializers for %qT", type);
1186 CONSTRUCTOR_ELTS (init) = v;
1190 /* Subroutine of process_init_constructor, which will process a single
1191 initializer INIT for a union of type TYPE. Returns the flags (PICFLAG_*)
1192 which describe the initializer. */
1195 process_init_constructor_union (tree type, tree init)
1197 constructor_elt *ce;
1200 /* If the initializer was empty, use default zero initialization. */
1201 if (VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (init)))
1204 len = VEC_length (constructor_elt, CONSTRUCTOR_ELTS (init));
1207 error ("too many initializers for %qT", type);
1208 VEC_block_remove (constructor_elt, CONSTRUCTOR_ELTS (init), 1, len-1);
1211 ce = VEC_index (constructor_elt, CONSTRUCTOR_ELTS (init), 0);
1213 /* If this element specifies a field, initialize via that field. */
1216 if (TREE_CODE (ce->index) == FIELD_DECL)
1218 else if (TREE_CODE (ce->index) == IDENTIFIER_NODE)
1220 /* This can happen within a cast, see g++.dg/opt/cse2.C. */
1221 tree name = ce->index;
1223 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1224 if (DECL_NAME (field) == name)
1228 error ("no field %qD found in union being initialized", field);
1229 ce->value = error_mark_node;
1235 gcc_assert (TREE_CODE (ce->index) == INTEGER_CST
1236 || TREE_CODE (ce->index) == RANGE_EXPR);
1237 error ("index value instead of field name in union initializer");
1238 ce->value = error_mark_node;
1243 /* Find the first named field. ANSI decided in September 1990
1244 that only named fields count here. */
1245 tree field = TYPE_FIELDS (type);
1246 while (field && (!DECL_NAME (field) || TREE_CODE (field) != FIELD_DECL))
1247 field = TREE_CHAIN (field);
1248 if (field == NULL_TREE)
1250 error ("too many initializers for %qT", type);
1251 ce->value = error_mark_node;
1256 if (ce->value && ce->value != error_mark_node)
1257 ce->value = digest_init_r (TREE_TYPE (ce->index), ce->value, true, LOOKUP_IMPLICIT);
1259 return picflag_from_initializer (ce->value);
1262 /* Process INIT, a constructor for a variable of aggregate type TYPE. The
1263 constructor is a brace-enclosed initializer, and will be modified in-place.
1265 Each element is converted to the right type through digest_init, and
1266 missing initializers are added following the language rules (zero-padding,
1269 After the execution, the initializer will have TREE_CONSTANT if all elts are
1270 constant, and TREE_STATIC set if, in addition, all elts are simple enough
1271 constants that the assembler and linker can compute them.
1273 The function returns the initializer itself, or error_mark_node in case
1277 process_init_constructor (tree type, tree init)
1281 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
1283 if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == VECTOR_TYPE)
1284 flags = process_init_constructor_array (type, init);
1285 else if (TREE_CODE (type) == RECORD_TYPE)
1286 flags = process_init_constructor_record (type, init);
1287 else if (TREE_CODE (type) == UNION_TYPE)
1288 flags = process_init_constructor_union (type, init);
1292 if (flags & PICFLAG_ERRONEOUS)
1293 return error_mark_node;
1295 TREE_TYPE (init) = type;
1296 if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == NULL_TREE)
1297 cp_complete_array_type (&TREE_TYPE (init), init, /*do_default=*/0);
1298 if (!(flags & PICFLAG_NOT_ALL_CONSTANT))
1300 TREE_CONSTANT (init) = 1;
1301 if (!(flags & PICFLAG_NOT_ALL_SIMPLE))
1302 TREE_STATIC (init) = 1;
1307 /* Given a structure or union value DATUM, construct and return
1308 the structure or union component which results from narrowing
1309 that value to the base specified in BASETYPE. For example, given the
1312 class L { int ii; };
1313 class A : L { ... };
1314 class B : L { ... };
1315 class C : A, B { ... };
1323 x.A::ii refers to the ii member of the L part of
1324 the A part of the C object named by X. In this case,
1325 DATUM would be x, and BASETYPE would be A.
1327 I used to think that this was nonconformant, that the standard specified
1328 that first we look up ii in A, then convert x to an L& and pull out the
1329 ii part. But in fact, it does say that we convert x to an A&; A here
1330 is known as the "naming class". (jason 2000-12-19)
1332 BINFO_P points to a variable initialized either to NULL_TREE or to the
1333 binfo for the specific base subobject we want to convert to. */
1336 build_scoped_ref (tree datum, tree basetype, tree* binfo_p)
1340 if (datum == error_mark_node)
1341 return error_mark_node;
1345 binfo = lookup_base (TREE_TYPE (datum), basetype, ba_check, NULL);
1347 if (!binfo || binfo == error_mark_node)
1349 *binfo_p = NULL_TREE;
1351 error_not_base_type (basetype, TREE_TYPE (datum));
1352 return error_mark_node;
1356 return build_base_path (PLUS_EXPR, datum, binfo, 1);
1359 /* Build a reference to an object specified by the C++ `->' operator.
1360 Usually this just involves dereferencing the object, but if the
1361 `->' operator is overloaded, then such overloads must be
1362 performed until an object which does not have the `->' operator
1363 overloaded is found. An error is reported when circular pointer
1364 delegation is detected. */
1367 build_x_arrow (tree expr)
1369 tree orig_expr = expr;
1370 tree type = TREE_TYPE (expr);
1371 tree last_rval = NULL_TREE;
1372 VEC(tree,gc) *types_memoized = NULL;
1374 if (type == error_mark_node)
1375 return error_mark_node;
1377 if (processing_template_decl)
1379 if (type_dependent_expression_p (expr))
1380 return build_min_nt (ARROW_EXPR, expr);
1381 expr = build_non_dependent_expr (expr);
1384 if (MAYBE_CLASS_TYPE_P (type))
1386 while ((expr = build_new_op (COMPONENT_REF, LOOKUP_NORMAL, expr,
1387 NULL_TREE, NULL_TREE,
1388 /*overloaded_p=*/NULL,
1389 tf_warning_or_error)))
1391 if (expr == error_mark_node)
1392 return error_mark_node;
1394 if (vec_member (TREE_TYPE (expr), types_memoized))
1396 error ("circular pointer delegation detected");
1397 return error_mark_node;
1400 VEC_safe_push (tree, gc, types_memoized, TREE_TYPE (expr));
1404 if (last_rval == NULL_TREE)
1406 error ("base operand of %<->%> has non-pointer type %qT", type);
1407 return error_mark_node;
1410 if (TREE_CODE (TREE_TYPE (last_rval)) == REFERENCE_TYPE)
1411 last_rval = convert_from_reference (last_rval);
1414 last_rval = decay_conversion (expr);
1416 if (TREE_CODE (TREE_TYPE (last_rval)) == POINTER_TYPE)
1418 if (processing_template_decl)
1420 expr = build_min_non_dep (ARROW_EXPR, last_rval, orig_expr);
1421 /* It will be dereferenced. */
1422 TREE_TYPE (expr) = TREE_TYPE (TREE_TYPE (last_rval));
1426 return cp_build_indirect_ref (last_rval, RO_NULL, tf_warning_or_error);
1430 error ("result of %<operator->()%> yields non-pointer result");
1432 error ("base operand of %<->%> is not a pointer");
1433 return error_mark_node;
1436 /* Return an expression for "DATUM .* COMPONENT". DATUM has not
1437 already been checked out to be of aggregate type. */
1440 build_m_component_ref (tree datum, tree component)
1448 if (error_operand_p (datum) || error_operand_p (component))
1449 return error_mark_node;
1451 datum = mark_lvalue_use (datum);
1452 component = mark_rvalue_use (component);
1454 ptrmem_type = TREE_TYPE (component);
1455 if (!TYPE_PTR_TO_MEMBER_P (ptrmem_type))
1457 error ("%qE cannot be used as a member pointer, since it is of "
1459 component, ptrmem_type);
1460 return error_mark_node;
1463 objtype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
1464 if (! MAYBE_CLASS_TYPE_P (objtype))
1466 error ("cannot apply member pointer %qE to %qE, which is of "
1467 "non-class type %qT",
1468 component, datum, objtype);
1469 return error_mark_node;
1472 type = TYPE_PTRMEM_POINTED_TO_TYPE (ptrmem_type);
1473 ctype = complete_type (TYPE_PTRMEM_CLASS_TYPE (ptrmem_type));
1475 if (!COMPLETE_TYPE_P (ctype))
1477 if (!same_type_p (ctype, objtype))
1483 binfo = lookup_base (objtype, ctype, ba_check, NULL);
1488 error ("pointer to member type %qT incompatible with object "
1491 return error_mark_node;
1493 else if (binfo == error_mark_node)
1494 return error_mark_node;
1497 if (TYPE_PTRMEM_P (ptrmem_type))
1501 /* Compute the type of the field, as described in [expr.ref].
1502 There's no such thing as a mutable pointer-to-member, so
1503 things are not as complex as they are for references to
1504 non-static data members. */
1505 type = cp_build_qualified_type (type,
1506 (cp_type_quals (type)
1507 | cp_type_quals (TREE_TYPE (datum))));
1509 datum = build_address (datum);
1511 /* Convert object to the correct base. */
1513 datum = build_base_path (PLUS_EXPR, datum, binfo, 1);
1515 /* Build an expression for "object + offset" where offset is the
1516 value stored in the pointer-to-data-member. */
1517 ptype = build_pointer_type (type);
1518 datum = build2 (POINTER_PLUS_EXPR, ptype,
1519 fold_convert (ptype, datum),
1520 build_nop (sizetype, component));
1521 return cp_build_indirect_ref (datum, RO_NULL, tf_warning_or_error);
1524 return build2 (OFFSET_REF, type, datum, component);
1527 /* Return a tree node for the expression TYPENAME '(' PARMS ')'. */
1530 build_functional_cast (tree exp, tree parms, tsubst_flags_t complain)
1532 /* This is either a call to a constructor,
1533 or a C cast in C++'s `functional' notation. */
1535 /* The type to which we are casting. */
1537 VEC(tree,gc) *parmvec;
1539 if (exp == error_mark_node || parms == error_mark_node)
1540 return error_mark_node;
1542 if (TREE_CODE (exp) == TYPE_DECL)
1543 type = TREE_TYPE (exp);
1547 /* We need to check this explicitly, since value-initialization of
1548 arrays is allowed in other situations. */
1549 if (TREE_CODE (type) == ARRAY_TYPE)
1551 if (complain & tf_error)
1552 error ("functional cast to array type %qT", type);
1553 return error_mark_node;
1556 if (processing_template_decl)
1560 /* Diagnose this even in a template. We could also try harder
1561 to give all the usual errors when the type and args are
1563 if (TREE_CODE (type) == REFERENCE_TYPE && !parms)
1565 if (complain & tf_error)
1566 error ("invalid value-initialization of reference type");
1567 return error_mark_node;
1570 t = build_min (CAST_EXPR, type, parms);
1571 /* We don't know if it will or will not have side effects. */
1572 TREE_SIDE_EFFECTS (t) = 1;
1576 if (! MAYBE_CLASS_TYPE_P (type))
1578 if (parms == NULL_TREE)
1580 if (VOID_TYPE_P (type))
1581 return void_zero_node;
1582 return build_value_init (type, complain);
1585 /* This must build a C cast. */
1586 parms = build_x_compound_expr_from_list (parms, ELK_FUNC_CAST, complain);
1587 return cp_build_c_cast (type, parms, complain);
1590 /* Prepare to evaluate as a call to a constructor. If this expression
1591 is actually used, for example,
1593 return X (arg1, arg2, ...);
1595 then the slot being initialized will be filled in. */
1597 if (!complete_type_or_maybe_complain (type, NULL_TREE, complain))
1598 return error_mark_node;
1599 if (abstract_virtuals_error_sfinae (NULL_TREE, type, complain))
1600 return error_mark_node;
1604 If the expression list is a single-expression, the type
1605 conversion is equivalent (in definedness, and if defined in
1606 meaning) to the corresponding cast expression. */
1607 if (parms && TREE_CHAIN (parms) == NULL_TREE)
1608 return cp_build_c_cast (type, TREE_VALUE (parms), complain);
1612 The expression T(), where T is a simple-type-specifier for a
1613 non-array complete object type or the (possibly cv-qualified)
1614 void type, creates an rvalue of the specified type, which is
1615 value-initialized. */
1617 if (parms == NULL_TREE
1618 /* If there's a user-defined constructor, value-initialization is
1619 just calling the constructor, so fall through. */
1620 && !TYPE_HAS_USER_CONSTRUCTOR (type))
1622 exp = build_value_init (type, complain);
1623 exp = get_target_expr_sfinae (exp, complain);
1624 /* FIXME this is wrong */
1625 if (literal_type_p (type))
1626 TREE_CONSTANT (exp) = true;
1630 /* Call the constructor. */
1631 parmvec = make_tree_vector ();
1632 for (; parms != NULL_TREE; parms = TREE_CHAIN (parms))
1633 VEC_safe_push (tree, gc, parmvec, TREE_VALUE (parms));
1634 exp = build_special_member_call (NULL_TREE, complete_ctor_identifier,
1635 &parmvec, type, LOOKUP_NORMAL, complain);
1636 release_tree_vector (parmvec);
1638 if (exp == error_mark_node)
1639 return error_mark_node;
1641 return build_cplus_new (type, exp, complain);
1645 /* Add new exception specifier SPEC, to the LIST we currently have.
1646 If it's already in LIST then do nothing.
1647 Moan if it's bad and we're allowed to. COMPLAIN < 0 means we
1648 know what we're doing. */
1651 add_exception_specifier (tree list, tree spec, int complain)
1656 diagnostic_t diag_type = DK_UNSPECIFIED; /* none */
1658 if (spec == error_mark_node)
1661 gcc_assert (spec && (!list || TREE_VALUE (list)));
1663 /* [except.spec] 1, type in an exception specifier shall not be
1664 incomplete, or pointer or ref to incomplete other than pointer
1666 is_ptr = TREE_CODE (core) == POINTER_TYPE;
1667 if (is_ptr || TREE_CODE (core) == REFERENCE_TYPE)
1668 core = TREE_TYPE (core);
1671 else if (VOID_TYPE_P (core))
1673 else if (TREE_CODE (core) == TEMPLATE_TYPE_PARM)
1675 else if (processing_template_decl)
1680 /* 15.4/1 says that types in an exception specifier must be complete,
1681 but it seems more reasonable to only require this on definitions
1682 and calls. So just give a pedwarn at this point; we will give an
1683 error later if we hit one of those two cases. */
1684 if (!COMPLETE_TYPE_P (complete_type (core)))
1685 diag_type = DK_PEDWARN; /* pedwarn */
1692 for (probe = list; probe; probe = TREE_CHAIN (probe))
1693 if (same_type_p (TREE_VALUE (probe), spec))
1696 list = tree_cons (NULL_TREE, spec, list);
1699 diag_type = DK_ERROR; /* error */
1701 if (diag_type != DK_UNSPECIFIED && complain)
1702 cxx_incomplete_type_diagnostic (NULL_TREE, core, diag_type);
1707 /* Combine the two exceptions specifier lists LIST and ADD, and return
1711 merge_exception_specifiers (tree list, tree add)
1715 /* For merging noexcept(true) and throw(), take the more recent one (LIST).
1716 A throw(type-list) spec takes precedence over a noexcept(false) spec.
1717 Any other noexcept-spec should only be merged with an equivalent one.
1718 So the !TREE_VALUE code below is correct for all cases. */
1719 else if (!TREE_VALUE (add))
1721 else if (!TREE_VALUE (list))
1725 tree orig_list = list;
1727 for (; add; add = TREE_CHAIN (add))
1729 tree spec = TREE_VALUE (add);
1732 for (probe = orig_list; probe; probe = TREE_CHAIN (probe))
1733 if (same_type_p (TREE_VALUE (probe), spec))
1737 spec = build_tree_list (NULL_TREE, spec);
1738 TREE_CHAIN (spec) = list;
1746 /* Subroutine of build_call. Ensure that each of the types in the
1747 exception specification is complete. Technically, 15.4/1 says that
1748 they need to be complete when we see a declaration of the function,
1749 but we should be able to get away with only requiring this when the
1750 function is defined or called. See also add_exception_specifier. */
1753 require_complete_eh_spec_types (tree fntype, tree decl)
1756 /* Don't complain about calls to op new. */
1757 if (decl && DECL_ARTIFICIAL (decl))
1759 for (raises = TYPE_RAISES_EXCEPTIONS (fntype); raises;
1760 raises = TREE_CHAIN (raises))
1762 tree type = TREE_VALUE (raises);
1763 if (type && !COMPLETE_TYPE_P (type))
1767 ("call to function %qD which throws incomplete type %q#T",
1770 error ("call to function which throws incomplete type %q#T",
1777 #include "gt-cp-typeck2.h"