2016-12-07 François Dumont <fdumont@gcc.gnu.org>
[official-gcc.git] / gcc / cp / typeck2.c
blobb214c99019caed9566470f087ec3009561ee81bd
1 /* Report error messages, build initializers, and perform
2 some front-end optimizations for C++ compiler.
3 Copyright (C) 1987-2016 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, tsubst_flags_t complain);
40 /* Print an error message stemming from an attempt to use
41 BASETYPE as a base class for TYPE. */
43 tree
44 error_not_base_type (tree basetype, tree type)
46 if (TREE_CODE (basetype) == FUNCTION_DECL)
47 basetype = DECL_CONTEXT (basetype);
48 error ("type %qT is not a base type for type %qT", basetype, type);
49 return error_mark_node;
52 tree
53 binfo_or_else (tree base, tree type)
55 tree binfo = lookup_base (type, base, ba_unique,
56 NULL, tf_warning_or_error);
58 if (binfo == error_mark_node)
59 return NULL_TREE;
60 else if (!binfo)
61 error_not_base_type (base, type);
62 return binfo;
65 /* According to ARM $7.1.6, "A `const' object may be initialized, but its
66 value may not be changed thereafter. */
68 void
69 cxx_readonly_error (tree arg, enum lvalue_use errstring)
72 /* This macro is used to emit diagnostics to ensure that all format
73 strings are complete sentences, visible to gettext and checked at
74 compile time. */
76 #define ERROR_FOR_ASSIGNMENT(AS, ASM, IN, DE, ARG) \
77 do { \
78 switch (errstring) \
79 { \
80 case lv_assign: \
81 error(AS, ARG); \
82 break; \
83 case lv_asm: \
84 error(ASM, ARG); \
85 break; \
86 case lv_increment: \
87 error (IN, ARG); \
88 break; \
89 case lv_decrement: \
90 error (DE, ARG); \
91 break; \
92 default: \
93 gcc_unreachable (); \
94 } \
95 } while (0)
97 /* Handle C++-specific things first. */
99 if (VAR_P (arg)
100 && DECL_LANG_SPECIFIC (arg)
101 && DECL_IN_AGGR_P (arg)
102 && !TREE_STATIC (arg))
103 ERROR_FOR_ASSIGNMENT (G_("assignment of "
104 "constant field %qD"),
105 G_("constant field %qD "
106 "used as %<asm%> output"),
107 G_("increment of "
108 "constant field %qD"),
109 G_("decrement of "
110 "constant field %qD"),
111 arg);
112 else if (INDIRECT_REF_P (arg)
113 && TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 0))) == REFERENCE_TYPE
114 && (VAR_P (TREE_OPERAND (arg, 0))
115 || TREE_CODE (TREE_OPERAND (arg, 0)) == PARM_DECL))
116 ERROR_FOR_ASSIGNMENT (G_("assignment of "
117 "read-only reference %qD"),
118 G_("read-only reference %qD "
119 "used as %<asm%> output"),
120 G_("increment of "
121 "read-only reference %qD"),
122 G_("decrement of "
123 "read-only reference %qD"),
124 TREE_OPERAND (arg, 0));
125 else
126 readonly_error (input_location, arg, errstring);
130 /* Structure that holds information about declarations whose type was
131 incomplete and we could not check whether it was abstract or not. */
133 struct GTY((chain_next ("%h.next"), for_user)) pending_abstract_type {
134 /* Declaration which we are checking for abstractness. It is either
135 a DECL node, or an IDENTIFIER_NODE if we do not have a full
136 declaration available. */
137 tree decl;
139 /* Type which will be checked for abstractness. */
140 tree type;
142 /* Kind of use in an unnamed declarator. */
143 enum abstract_class_use use;
145 /* Position of the declaration. This is only needed for IDENTIFIER_NODEs,
146 because DECLs already carry locus information. */
147 location_t locus;
149 /* Link to the next element in list. */
150 struct pending_abstract_type* next;
153 struct abstract_type_hasher : ggc_ptr_hash<pending_abstract_type>
155 typedef tree compare_type;
156 static hashval_t hash (pending_abstract_type *);
157 static bool equal (pending_abstract_type *, tree);
160 /* Compute the hash value of the node VAL. This function is used by the
161 hash table abstract_pending_vars. */
163 hashval_t
164 abstract_type_hasher::hash (pending_abstract_type *pat)
166 return (hashval_t) TYPE_UID (pat->type);
170 /* Compare node VAL1 with the type VAL2. This function is used by the
171 hash table abstract_pending_vars. */
173 bool
174 abstract_type_hasher::equal (pending_abstract_type *pat1, tree type2)
176 return (pat1->type == type2);
179 /* Hash table that maintains pending_abstract_type nodes, for which we still
180 need to check for type abstractness. The key of the table is the type
181 of the declaration. */
182 static GTY (()) hash_table<abstract_type_hasher> *abstract_pending_vars = NULL;
184 static int abstract_virtuals_error_sfinae (tree, tree, abstract_class_use, tsubst_flags_t);
186 /* This function is called after TYPE is completed, and will check if there
187 are pending declarations for which we still need to verify the abstractness
188 of TYPE, and emit a diagnostic (through abstract_virtuals_error) if TYPE
189 turned out to be incomplete. */
191 void
192 complete_type_check_abstract (tree type)
194 struct pending_abstract_type *pat;
195 location_t cur_loc = input_location;
197 gcc_assert (COMPLETE_TYPE_P (type));
199 if (!abstract_pending_vars)
200 return;
202 /* Retrieve the list of pending declarations for this type. */
203 pending_abstract_type **slot
204 = abstract_pending_vars->find_slot_with_hash (type, TYPE_UID (type),
205 NO_INSERT);
206 if (!slot)
207 return;
208 pat = *slot;
209 gcc_assert (pat);
211 /* If the type is not abstract, do not do anything. */
212 if (CLASSTYPE_PURE_VIRTUALS (type))
214 struct pending_abstract_type *prev = 0, *next;
216 /* Reverse the list to emit the errors in top-down order. */
217 for (; pat; pat = next)
219 next = pat->next;
220 pat->next = prev;
221 prev = pat;
223 pat = prev;
225 /* Go through the list, and call abstract_virtuals_error for each
226 element: it will issue a diagnostic if the type is abstract. */
227 while (pat)
229 gcc_assert (type == pat->type);
231 /* Tweak input_location so that the diagnostic appears at the correct
232 location. Notice that this is only needed if the decl is an
233 IDENTIFIER_NODE. */
234 input_location = pat->locus;
235 abstract_virtuals_error_sfinae (pat->decl, pat->type, pat->use,
236 tf_warning_or_error);
237 pat = pat->next;
241 abstract_pending_vars->clear_slot (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, in which case USE specifies
250 the kind of invalid use. Returns 1 if an error occurred; zero if
251 all was well. */
253 static int
254 abstract_virtuals_error_sfinae (tree decl, tree type, abstract_class_use use,
255 tsubst_flags_t complain)
257 vec<tree, va_gc> *pure;
259 /* This function applies only to classes. Any other entity can never
260 be abstract. */
261 if (!CLASS_TYPE_P (type))
262 return 0;
263 type = TYPE_MAIN_VARIANT (type);
265 #if 0
266 /* Instantiation here seems to be required by the standard,
267 but breaks e.g. boost::bind. FIXME! */
268 /* In SFINAE, non-N3276 context, force instantiation. */
269 if (!(complain & (tf_error|tf_decltype)))
270 complete_type (type);
271 #endif
273 /* If the type is incomplete, we register it within a hash table,
274 so that we can check again once it is completed. This makes sense
275 only for objects for which we have a declaration or at least a
276 name. */
277 if (!COMPLETE_TYPE_P (type) && (complain & tf_error))
279 struct pending_abstract_type *pat;
281 gcc_assert (!decl || DECL_P (decl) || identifier_p (decl));
283 if (!abstract_pending_vars)
284 abstract_pending_vars
285 = hash_table<abstract_type_hasher>::create_ggc (31);
287 pending_abstract_type **slot
288 = abstract_pending_vars->find_slot_with_hash (type, TYPE_UID (type),
289 INSERT);
291 pat = ggc_alloc<pending_abstract_type> ();
292 pat->type = type;
293 pat->decl = decl;
294 pat->use = use;
295 pat->locus = ((decl && DECL_P (decl))
296 ? DECL_SOURCE_LOCATION (decl)
297 : input_location);
299 pat->next = *slot;
300 *slot = pat;
302 return 0;
305 if (!TYPE_SIZE (type))
306 /* TYPE is being defined, and during that time
307 CLASSTYPE_PURE_VIRTUALS holds the inline friends. */
308 return 0;
310 pure = CLASSTYPE_PURE_VIRTUALS (type);
311 if (!pure)
312 return 0;
314 if (!(complain & tf_error))
315 return 1;
317 if (decl)
319 if (VAR_P (decl))
320 error ("cannot declare variable %q+D to be of abstract "
321 "type %qT", decl, type);
322 else if (TREE_CODE (decl) == PARM_DECL)
324 if (DECL_NAME (decl))
325 error ("cannot declare parameter %q+D to be of abstract type %qT",
326 decl, type);
327 else
328 error ("cannot declare parameter to be of abstract type %qT",
329 type);
331 else if (TREE_CODE (decl) == FIELD_DECL)
332 error ("cannot declare field %q+D to be of abstract type %qT",
333 decl, type);
334 else if (TREE_CODE (decl) == FUNCTION_DECL
335 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
336 error ("invalid abstract return type for member function %q+#D", decl);
337 else if (TREE_CODE (decl) == FUNCTION_DECL)
338 error ("invalid abstract return type for function %q+#D", decl);
339 else if (identifier_p (decl))
340 /* Here we do not have location information. */
341 error ("invalid abstract type %qT for %qE", type, decl);
342 else
343 error ("invalid abstract type for %q+D", decl);
345 else switch (use)
347 case ACU_ARRAY:
348 error ("creating array of %qT, which is an abstract class type", type);
349 break;
350 case ACU_CAST:
351 error ("invalid cast to abstract class type %qT", type);
352 break;
353 case ACU_NEW:
354 error ("invalid new-expression of abstract class type %qT", type);
355 break;
356 case ACU_RETURN:
357 error ("invalid abstract return type %qT", type);
358 break;
359 case ACU_PARM:
360 error ("invalid abstract parameter type %qT", type);
361 break;
362 case ACU_THROW:
363 error ("expression of abstract class type %qT cannot "
364 "be used in throw-expression", type);
365 break;
366 case ACU_CATCH:
367 error ("cannot declare catch parameter to be of abstract "
368 "class type %qT", type);
369 break;
370 default:
371 error ("cannot allocate an object of abstract type %qT", type);
374 /* Only go through this once. */
375 if (pure->length ())
377 unsigned ix;
378 tree fn;
380 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
381 " because the following virtual functions are pure within %qT:",
382 type);
384 FOR_EACH_VEC_ELT (*pure, ix, fn)
385 if (! DECL_CLONED_FUNCTION_P (fn)
386 || DECL_COMPLETE_DESTRUCTOR_P (fn))
387 inform (DECL_SOURCE_LOCATION (fn), "\t%#D", fn);
389 /* Now truncate the vector. This leaves it non-null, so we know
390 there are pure virtuals, but empty so we don't list them out
391 again. */
392 pure->truncate (0);
395 return 1;
399 abstract_virtuals_error_sfinae (tree decl, tree type, tsubst_flags_t complain)
401 return abstract_virtuals_error_sfinae (decl, type, ACU_UNKNOWN, complain);
405 abstract_virtuals_error_sfinae (abstract_class_use use, tree type,
406 tsubst_flags_t complain)
408 return abstract_virtuals_error_sfinae (NULL_TREE, type, use, complain);
412 /* Wrapper for the above function in the common case of wanting errors. */
415 abstract_virtuals_error (tree decl, tree type)
417 return abstract_virtuals_error_sfinae (decl, type, tf_warning_or_error);
421 abstract_virtuals_error (abstract_class_use use, tree type)
423 return abstract_virtuals_error_sfinae (use, type, tf_warning_or_error);
426 /* Print an inform about the declaration of the incomplete type TYPE. */
428 void
429 cxx_incomplete_type_inform (const_tree type)
431 if (!TYPE_MAIN_DECL (type))
432 return;
434 location_t loc = DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type));
435 tree ptype = strip_top_quals (CONST_CAST_TREE (type));
437 if (current_class_type
438 && TYPE_BEING_DEFINED (current_class_type)
439 && same_type_p (ptype, current_class_type))
440 inform (loc, "definition of %q#T is not complete until "
441 "the closing brace", ptype);
442 else if (!TYPE_TEMPLATE_INFO (ptype))
443 inform (loc, "forward declaration of %q#T", ptype);
444 else
445 inform (loc, "declaration of %q#T", ptype);
448 /* Print an error message for invalid use of an incomplete type.
449 VALUE is the expression that was used (or 0 if that isn't known)
450 and TYPE is the type that was invalid. DIAG_KIND indicates the
451 type of diagnostic (see diagnostic.def). */
453 void
454 cxx_incomplete_type_diagnostic (location_t loc, const_tree value,
455 const_tree type, diagnostic_t diag_kind)
457 bool is_decl = false, complained = false;
459 gcc_assert (diag_kind == DK_WARNING
460 || diag_kind == DK_PEDWARN
461 || diag_kind == DK_ERROR);
463 /* Avoid duplicate error message. */
464 if (TREE_CODE (type) == ERROR_MARK)
465 return;
467 if (value != 0 && (VAR_P (value)
468 || TREE_CODE (value) == PARM_DECL
469 || TREE_CODE (value) == FIELD_DECL))
471 complained = emit_diagnostic (diag_kind, DECL_SOURCE_LOCATION (value), 0,
472 "%qD has incomplete type", value);
473 is_decl = true;
475 retry:
476 /* We must print an error message. Be clever about what it says. */
478 switch (TREE_CODE (type))
480 case RECORD_TYPE:
481 case UNION_TYPE:
482 case ENUMERAL_TYPE:
483 if (!is_decl)
484 complained = emit_diagnostic (diag_kind, loc, 0,
485 "invalid use of incomplete type %q#T",
486 type);
487 if (complained)
488 cxx_incomplete_type_inform (type);
489 break;
491 case VOID_TYPE:
492 emit_diagnostic (diag_kind, loc, 0,
493 "invalid use of %qT", type);
494 break;
496 case ARRAY_TYPE:
497 if (TYPE_DOMAIN (type))
499 type = TREE_TYPE (type);
500 goto retry;
502 emit_diagnostic (diag_kind, loc, 0,
503 "invalid use of array with unspecified bounds");
504 break;
506 case OFFSET_TYPE:
507 bad_member:
509 tree member = TREE_OPERAND (value, 1);
510 if (is_overloaded_fn (member))
511 member = get_first_fn (member);
512 if (DECL_FUNCTION_MEMBER_P (member)
513 && ! flag_ms_extensions)
514 emit_diagnostic (diag_kind, loc, 0,
515 "invalid use of member function %qD "
516 "(did you forget the %<()%> ?)", member);
517 else
518 emit_diagnostic (diag_kind, loc, 0,
519 "invalid use of member %qD "
520 "(did you forget the %<&%> ?)", member);
522 break;
524 case TEMPLATE_TYPE_PARM:
525 if (is_auto (type))
526 emit_diagnostic (diag_kind, loc, 0,
527 "invalid use of %<auto%>");
528 else
529 emit_diagnostic (diag_kind, loc, 0,
530 "invalid use of template type parameter %qT", type);
531 break;
533 case BOUND_TEMPLATE_TEMPLATE_PARM:
534 emit_diagnostic (diag_kind, loc, 0,
535 "invalid use of template template parameter %qT",
536 TYPE_NAME (type));
537 break;
539 case TYPENAME_TYPE:
540 case DECLTYPE_TYPE:
541 emit_diagnostic (diag_kind, loc, 0,
542 "invalid use of dependent type %qT", type);
543 break;
545 case LANG_TYPE:
546 if (type == init_list_type_node)
548 emit_diagnostic (diag_kind, loc, 0,
549 "invalid use of brace-enclosed initializer list");
550 break;
552 gcc_assert (type == unknown_type_node);
553 if (value && TREE_CODE (value) == COMPONENT_REF)
554 goto bad_member;
555 else if (value && TREE_CODE (value) == ADDR_EXPR)
556 emit_diagnostic (diag_kind, loc, 0,
557 "address of overloaded function with no contextual "
558 "type information");
559 else if (value && TREE_CODE (value) == OVERLOAD)
560 emit_diagnostic (diag_kind, loc, 0,
561 "overloaded function with no contextual type information");
562 else
563 emit_diagnostic (diag_kind, loc, 0,
564 "insufficient contextual information to determine type");
565 break;
567 default:
568 gcc_unreachable ();
572 /* Print an error message for invalid use of an incomplete type.
573 VALUE is the expression that was used (or 0 if that isn't known)
574 and TYPE is the type that was invalid. */
576 void
577 cxx_incomplete_type_error (location_t loc, const_tree value, const_tree type)
579 cxx_incomplete_type_diagnostic (loc, value, type, DK_ERROR);
583 /* The recursive part of split_nonconstant_init. DEST is an lvalue
584 expression to which INIT should be assigned. INIT is a CONSTRUCTOR.
585 Return true if the whole of the value was initialized by the
586 generated statements. */
588 static bool
589 split_nonconstant_init_1 (tree dest, tree init)
591 unsigned HOST_WIDE_INT idx;
592 tree field_index, value;
593 tree type = TREE_TYPE (dest);
594 tree inner_type = NULL;
595 bool array_type_p = false;
596 bool complete_p = true;
597 HOST_WIDE_INT num_split_elts = 0;
599 switch (TREE_CODE (type))
601 case ARRAY_TYPE:
602 inner_type = TREE_TYPE (type);
603 array_type_p = true;
604 if ((TREE_SIDE_EFFECTS (init)
605 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
606 || array_of_runtime_bound_p (type))
608 /* For an array, we only need/want a single cleanup region rather
609 than one per element. */
610 tree code = build_vec_init (dest, NULL_TREE, init, false, 1,
611 tf_warning_or_error);
612 add_stmt (code);
613 return true;
615 /* FALLTHRU */
617 case RECORD_TYPE:
618 case UNION_TYPE:
619 case QUAL_UNION_TYPE:
620 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), idx,
621 field_index, value)
623 /* The current implementation of this algorithm assumes that
624 the field was set for all the elements. This is usually done
625 by process_init_constructor. */
626 gcc_assert (field_index);
628 if (!array_type_p)
629 inner_type = TREE_TYPE (field_index);
631 if (TREE_CODE (value) == CONSTRUCTOR)
633 tree sub;
635 if (array_type_p)
636 sub = build4 (ARRAY_REF, inner_type, dest, field_index,
637 NULL_TREE, NULL_TREE);
638 else
639 sub = build3 (COMPONENT_REF, inner_type, dest, field_index,
640 NULL_TREE);
642 if (!split_nonconstant_init_1 (sub, value))
643 complete_p = false;
644 else
645 CONSTRUCTOR_ELTS (init)->ordered_remove (idx--);
646 num_split_elts++;
648 else if (!initializer_constant_valid_p (value, inner_type))
650 tree code;
651 tree sub;
653 /* FIXME: Ordered removal is O(1) so the whole function is
654 worst-case quadratic. This could be fixed using an aside
655 bitmap to record which elements must be removed and remove
656 them all at the same time. Or by merging
657 split_non_constant_init into process_init_constructor_array,
658 that is separating constants from non-constants while building
659 the vector. */
660 CONSTRUCTOR_ELTS (init)->ordered_remove (idx);
661 --idx;
663 if (TREE_CODE (field_index) == RANGE_EXPR)
665 /* Use build_vec_init to initialize a range. */
666 tree low = TREE_OPERAND (field_index, 0);
667 tree hi = TREE_OPERAND (field_index, 1);
668 sub = build4 (ARRAY_REF, inner_type, dest, low,
669 NULL_TREE, NULL_TREE);
670 sub = cp_build_addr_expr (sub, tf_warning_or_error);
671 tree max = size_binop (MINUS_EXPR, hi, low);
672 code = build_vec_init (sub, max, value, false, 0,
673 tf_warning_or_error);
674 add_stmt (code);
675 if (tree_fits_shwi_p (max))
676 num_split_elts += tree_to_shwi (max);
678 else
680 if (array_type_p)
681 sub = build4 (ARRAY_REF, inner_type, dest, field_index,
682 NULL_TREE, NULL_TREE);
683 else
684 sub = build3 (COMPONENT_REF, inner_type, dest, field_index,
685 NULL_TREE);
687 code = build2 (INIT_EXPR, inner_type, sub, value);
688 code = build_stmt (input_location, EXPR_STMT, code);
689 code = maybe_cleanup_point_expr_void (code);
690 add_stmt (code);
691 if (tree cleanup
692 = cxx_maybe_build_cleanup (sub, tf_warning_or_error))
693 finish_eh_cleanup (cleanup);
696 num_split_elts++;
699 break;
701 case VECTOR_TYPE:
702 if (!initializer_constant_valid_p (init, type))
704 tree code;
705 tree cons = copy_node (init);
706 CONSTRUCTOR_ELTS (init) = NULL;
707 code = build2 (MODIFY_EXPR, type, dest, cons);
708 code = build_stmt (input_location, EXPR_STMT, code);
709 add_stmt (code);
710 num_split_elts += CONSTRUCTOR_NELTS (init);
712 break;
714 default:
715 gcc_unreachable ();
718 /* The rest of the initializer is now a constant. */
719 TREE_CONSTANT (init) = 1;
720 return complete_p && complete_ctor_at_level_p (TREE_TYPE (init),
721 num_split_elts, inner_type);
724 /* A subroutine of store_init_value. Splits non-constant static
725 initializer INIT into a constant part and generates code to
726 perform the non-constant part of the initialization to DEST.
727 Returns the code for the runtime init. */
729 tree
730 split_nonconstant_init (tree dest, tree init)
732 tree code;
734 if (TREE_CODE (init) == TARGET_EXPR)
735 init = TARGET_EXPR_INITIAL (init);
736 if (TREE_CODE (init) == CONSTRUCTOR)
738 init = cp_fully_fold (init);
739 code = push_stmt_list ();
740 if (split_nonconstant_init_1 (dest, init))
741 init = NULL_TREE;
742 code = pop_stmt_list (code);
743 DECL_INITIAL (dest) = init;
744 TREE_READONLY (dest) = 0;
746 else
747 code = build2 (INIT_EXPR, TREE_TYPE (dest), dest, init);
749 return code;
752 /* Perform appropriate conversions on the initial value of a variable,
753 store it in the declaration DECL,
754 and print any error messages that are appropriate.
755 If the init is invalid, store an ERROR_MARK.
757 C++: Note that INIT might be a TREE_LIST, which would mean that it is
758 a base class initializer for some aggregate type, hopefully compatible
759 with DECL. If INIT is a single element, and DECL is an aggregate
760 type, we silently convert INIT into a TREE_LIST, allowing a constructor
761 to be called.
763 If INIT is a TREE_LIST and there is no constructor, turn INIT
764 into a CONSTRUCTOR and use standard initialization techniques.
765 Perhaps a warning should be generated?
767 Returns code to be executed if initialization could not be performed
768 for static variable. In that case, caller must emit the code. */
770 tree
771 store_init_value (tree decl, tree init, vec<tree, va_gc>** cleanups, int flags)
773 tree value, type;
775 /* If variable's type was invalidly declared, just ignore it. */
777 type = TREE_TYPE (decl);
778 if (TREE_CODE (type) == ERROR_MARK)
779 return NULL_TREE;
781 if (MAYBE_CLASS_TYPE_P (type))
783 if (TREE_CODE (init) == TREE_LIST)
785 error ("constructor syntax used, but no constructor declared "
786 "for type %qT", type);
787 init = build_constructor_from_list (init_list_type_node, nreverse (init));
791 /* End of special C++ code. */
793 if (flags & LOOKUP_ALREADY_DIGESTED)
794 value = init;
795 else
796 /* Digest the specified initializer into an expression. */
797 value = digest_init_flags (type, init, flags, tf_warning_or_error);
799 value = extend_ref_init_temps (decl, value, cleanups);
801 /* In C++11 constant expression is a semantic, not syntactic, property.
802 In C++98, make sure that what we thought was a constant expression at
803 template definition time is still constant and otherwise perform this
804 as optimization, e.g. to fold SIZEOF_EXPRs in the initializer. */
805 if (decl_maybe_constant_var_p (decl) || TREE_STATIC (decl))
807 bool const_init;
808 value = instantiate_non_dependent_expr (value);
809 if (DECL_DECLARED_CONSTEXPR_P (decl)
810 || (DECL_IN_AGGR_P (decl) && !DECL_VAR_DECLARED_INLINE_P (decl)))
812 /* Diagnose a non-constant initializer for constexpr. */
813 if (processing_template_decl
814 && !require_potential_constant_expression (value))
815 value = error_mark_node;
816 else
817 value = cxx_constant_value (value, decl);
819 value = maybe_constant_init (value, decl);
820 if (TREE_CODE (value) == CONSTRUCTOR && cp_has_mutable_p (type))
821 /* Poison this CONSTRUCTOR so it can't be copied to another
822 constexpr variable. */
823 CONSTRUCTOR_MUTABLE_POISON (value) = true;
824 const_init = (reduced_constant_expression_p (value)
825 || error_operand_p (value));
826 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = const_init;
827 /* FIXME setting TREE_CONSTANT on refs breaks the back end. */
828 if (TREE_CODE (type) != REFERENCE_TYPE)
829 TREE_CONSTANT (decl) = const_init && decl_maybe_constant_var_p (decl);
831 value = cp_fully_fold (value);
833 if (cxx_dialect >= cxx14 && CLASS_TYPE_P (strip_array_types (type)))
834 /* Handle aggregate NSDMI in non-constant initializers, too. */
835 value = replace_placeholders (value, decl);
837 /* DECL may change value; purge caches. */
838 clear_cv_and_fold_caches ();
840 /* If the initializer is not a constant, fill in DECL_INITIAL with
841 the bits that are constant, and then return an expression that
842 will perform the dynamic initialization. */
843 if (value != error_mark_node
844 && (TREE_SIDE_EFFECTS (value)
845 || array_of_runtime_bound_p (type)
846 || ! reduced_constant_expression_p (value)))
847 return split_nonconstant_init (decl, value);
848 /* If the value is a constant, just put it in DECL_INITIAL. If DECL
849 is an automatic variable, the middle end will turn this into a
850 dynamic initialization later. */
851 DECL_INITIAL (decl) = value;
852 return NULL_TREE;
856 /* Give diagnostic about narrowing conversions within { }. */
858 bool
859 check_narrowing (tree type, tree init, tsubst_flags_t complain)
861 tree ftype = unlowered_expr_type (init);
862 bool ok = true;
863 REAL_VALUE_TYPE d;
865 if (((!warn_narrowing || !(complain & tf_warning))
866 && cxx_dialect == cxx98)
867 || !ARITHMETIC_TYPE_P (type))
868 return ok;
870 if (BRACE_ENCLOSED_INITIALIZER_P (init)
871 && TREE_CODE (type) == COMPLEX_TYPE)
873 tree elttype = TREE_TYPE (type);
874 if (CONSTRUCTOR_NELTS (init) > 0)
875 ok &= check_narrowing (elttype, CONSTRUCTOR_ELT (init, 0)->value,
876 complain);
877 if (CONSTRUCTOR_NELTS (init) > 1)
878 ok &= check_narrowing (elttype, CONSTRUCTOR_ELT (init, 1)->value,
879 complain);
880 return ok;
883 init = fold_non_dependent_expr (init);
885 if (TREE_CODE (type) == INTEGER_TYPE
886 && TREE_CODE (ftype) == REAL_TYPE)
887 ok = false;
888 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype)
889 && CP_INTEGRAL_TYPE_P (type))
891 if (TREE_CODE (ftype) == ENUMERAL_TYPE)
892 /* Check for narrowing based on the values of the enumeration. */
893 ftype = ENUM_UNDERLYING_TYPE (ftype);
894 if ((tree_int_cst_lt (TYPE_MAX_VALUE (type),
895 TYPE_MAX_VALUE (ftype))
896 || tree_int_cst_lt (TYPE_MIN_VALUE (ftype),
897 TYPE_MIN_VALUE (type)))
898 && (TREE_CODE (init) != INTEGER_CST
899 || !int_fits_type_p (init, type)))
900 ok = false;
902 else if (TREE_CODE (ftype) == REAL_TYPE
903 && TREE_CODE (type) == REAL_TYPE)
905 if (TYPE_PRECISION (type) < TYPE_PRECISION (ftype))
907 if (TREE_CODE (init) == REAL_CST)
909 /* Issue 703: Loss of precision is OK as long as the value is
910 within the representable range of the new type. */
911 REAL_VALUE_TYPE r;
912 d = TREE_REAL_CST (init);
913 real_convert (&r, TYPE_MODE (type), &d);
914 if (real_isinf (&r))
915 ok = false;
917 else
918 ok = false;
921 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype)
922 && TREE_CODE (type) == REAL_TYPE)
924 ok = false;
925 if (TREE_CODE (init) == INTEGER_CST)
927 d = real_value_from_int_cst (0, init);
928 if (exact_real_truncate (TYPE_MODE (type), &d))
929 ok = true;
933 bool almost_ok = ok;
934 if (!ok && !CONSTANT_CLASS_P (init) && (complain & tf_warning_or_error))
936 tree folded = cp_fully_fold (init);
937 if (TREE_CONSTANT (folded) && check_narrowing (type, folded, tf_none))
938 almost_ok = true;
941 if (!ok)
943 location_t loc = EXPR_LOC_OR_LOC (init, input_location);
944 if (cxx_dialect == cxx98)
946 if (complain & tf_warning)
947 warning_at (loc, OPT_Wnarrowing, "narrowing conversion of %qE "
948 "from %qT to %qT inside { } is ill-formed in C++11",
949 init, ftype, type);
950 ok = true;
952 else if (!CONSTANT_CLASS_P (init))
954 if (complain & tf_warning_or_error)
956 if ((!almost_ok || pedantic)
957 && pedwarn (loc, OPT_Wnarrowing,
958 "narrowing conversion of %qE "
959 "from %qT to %qT inside { }",
960 init, ftype, type)
961 && almost_ok)
962 inform (loc, " the expression has a constant value but is not "
963 "a C++ constant-expression");
964 ok = true;
967 else if (complain & tf_error)
969 int savederrorcount = errorcount;
970 global_dc->pedantic_errors = 1;
971 pedwarn (loc, OPT_Wnarrowing,
972 "narrowing conversion of %qE from %qT to %qT "
973 "inside { }", init, ftype, type);
974 if (errorcount == savederrorcount)
975 ok = true;
976 global_dc->pedantic_errors = flag_pedantic_errors;
980 return ok;
983 /* Process the initializer INIT for a variable of type TYPE, emitting
984 diagnostics for invalid initializers and converting the initializer as
985 appropriate.
987 For aggregate types, it assumes that reshape_init has already run, thus the
988 initializer will have the right shape (brace elision has been undone).
990 NESTED is true iff we are being called for an element of a CONSTRUCTOR. */
992 static tree
993 digest_init_r (tree type, tree init, bool nested, int flags,
994 tsubst_flags_t complain)
996 enum tree_code code = TREE_CODE (type);
998 if (error_operand_p (init))
999 return error_mark_node;
1001 gcc_assert (init);
1003 /* We must strip the outermost array type when completing the type,
1004 because the its bounds might be incomplete at the moment. */
1005 if (!complete_type_or_maybe_complain (TREE_CODE (type) == ARRAY_TYPE
1006 ? TREE_TYPE (type) : type, NULL_TREE,
1007 complain))
1008 return error_mark_node;
1010 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue
1011 (g++.old-deja/g++.law/casts2.C). */
1012 if (TREE_CODE (init) == NON_LVALUE_EXPR)
1013 init = TREE_OPERAND (init, 0);
1015 location_t loc = EXPR_LOC_OR_LOC (init, input_location);
1017 /* Initialization of an array of chars from a string constant. The initializer
1018 can be optionally enclosed in braces, but reshape_init has already removed
1019 them if they were present. */
1020 if (code == ARRAY_TYPE)
1022 if (nested && !TYPE_DOMAIN (type))
1024 /* C++ flexible array members have a null domain. */
1025 pedwarn (loc, OPT_Wpedantic,
1026 "initialization of a flexible array member");
1029 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
1030 if (char_type_p (typ1)
1031 /*&& init */
1032 && TREE_CODE (init) == STRING_CST)
1034 tree char_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (init)));
1036 if (TYPE_PRECISION (typ1) == BITS_PER_UNIT)
1038 if (char_type != char_type_node)
1040 if (complain & tf_error)
1041 error_at (loc, "char-array initialized from wide string");
1042 return error_mark_node;
1045 else
1047 if (char_type == char_type_node)
1049 if (complain & tf_error)
1050 error_at (loc,
1051 "int-array initialized from non-wide string");
1052 return error_mark_node;
1054 else if (char_type != typ1)
1056 if (complain & tf_error)
1057 error_at (loc, "int-array initialized from incompatible "
1058 "wide string");
1059 return error_mark_node;
1063 if (type != TREE_TYPE (init))
1065 init = copy_node (init);
1066 TREE_TYPE (init) = type;
1068 if (TYPE_DOMAIN (type) && TREE_CONSTANT (TYPE_SIZE (type)))
1070 /* Not a flexible array member. */
1071 int size = TREE_INT_CST_LOW (TYPE_SIZE (type));
1072 size = (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
1073 /* In C it is ok to subtract 1 from the length of the string
1074 because it's ok to ignore the terminating null char that is
1075 counted in the length of the constant, but in C++ this would
1076 be invalid. */
1077 if (size < TREE_STRING_LENGTH (init))
1078 permerror (loc, "initializer-string for array "
1079 "of chars is too long");
1081 return init;
1085 /* Handle scalar types (including conversions) and references. */
1086 if ((TREE_CODE (type) != COMPLEX_TYPE
1087 || BRACE_ENCLOSED_INITIALIZER_P (init))
1088 && (SCALAR_TYPE_P (type) || code == REFERENCE_TYPE))
1090 if (nested)
1091 flags |= LOOKUP_NO_NARROWING;
1092 init = convert_for_initialization (0, type, init, flags,
1093 ICR_INIT, NULL_TREE, 0,
1094 complain);
1096 return init;
1099 /* Come here only for aggregates: records, arrays, unions, complex numbers
1100 and vectors. */
1101 gcc_assert (TREE_CODE (type) == ARRAY_TYPE
1102 || VECTOR_TYPE_P (type)
1103 || TREE_CODE (type) == RECORD_TYPE
1104 || TREE_CODE (type) == UNION_TYPE
1105 || TREE_CODE (type) == COMPLEX_TYPE);
1107 /* "If T is a class type and the initializer list has a single
1108 element of type cv U, where U is T or a class derived from T,
1109 the object is initialized from that element." */
1110 if (flag_checking
1111 && cxx_dialect >= cxx11
1112 && BRACE_ENCLOSED_INITIALIZER_P (init)
1113 && CONSTRUCTOR_NELTS (init) == 1
1114 && ((CLASS_TYPE_P (type) && !CLASSTYPE_NON_AGGREGATE (type))
1115 || VECTOR_TYPE_P (type)))
1117 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
1118 if (reference_related_p (type, TREE_TYPE (elt)))
1119 /* We should have fixed this in reshape_init. */
1120 gcc_unreachable ();
1123 if (BRACE_ENCLOSED_INITIALIZER_P (init)
1124 && !TYPE_NON_AGGREGATE_CLASS (type))
1125 return process_init_constructor (type, init, complain);
1126 else
1128 if (COMPOUND_LITERAL_P (init) && TREE_CODE (type) == ARRAY_TYPE)
1130 if (complain & tf_error)
1131 error_at (loc, "cannot initialize aggregate of type %qT with "
1132 "a compound literal", type);
1134 return error_mark_node;
1137 if (TREE_CODE (type) == ARRAY_TYPE
1138 && !BRACE_ENCLOSED_INITIALIZER_P (init))
1140 /* Allow the result of build_array_copy and of
1141 build_value_init_noctor. */
1142 if ((TREE_CODE (init) == VEC_INIT_EXPR
1143 || TREE_CODE (init) == CONSTRUCTOR)
1144 && (same_type_ignoring_top_level_qualifiers_p
1145 (type, TREE_TYPE (init))))
1146 return init;
1148 if (complain & tf_error)
1149 error_at (loc, "array must be initialized with a brace-enclosed"
1150 " initializer");
1151 return error_mark_node;
1154 return convert_for_initialization (NULL_TREE, type, init,
1155 flags,
1156 ICR_INIT, NULL_TREE, 0,
1157 complain);
1161 tree
1162 digest_init (tree type, tree init, tsubst_flags_t complain)
1164 return digest_init_r (type, init, false, LOOKUP_IMPLICIT, complain);
1167 tree
1168 digest_init_flags (tree type, tree init, int flags, tsubst_flags_t complain)
1170 return digest_init_r (type, init, false, flags, complain);
1173 /* Process the initializer INIT for an NSDMI DECL (a FIELD_DECL). */
1174 tree
1175 digest_nsdmi_init (tree decl, tree init)
1177 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
1179 tree type = TREE_TYPE (decl);
1180 int flags = LOOKUP_IMPLICIT;
1181 if (DIRECT_LIST_INIT_P (init))
1182 flags = LOOKUP_NORMAL;
1183 if (BRACE_ENCLOSED_INITIALIZER_P (init)
1184 && CP_AGGREGATE_TYPE_P (type))
1185 init = reshape_init (type, init, tf_warning_or_error);
1186 init = digest_init_flags (type, init, flags, tf_warning_or_error);
1187 if (TREE_CODE (init) == TARGET_EXPR)
1188 /* This represents the whole initialization. */
1189 TARGET_EXPR_DIRECT_INIT_P (init) = true;
1190 return init;
1193 /* Set of flags used within process_init_constructor to describe the
1194 initializers. */
1195 #define PICFLAG_ERRONEOUS 1
1196 #define PICFLAG_NOT_ALL_CONSTANT 2
1197 #define PICFLAG_NOT_ALL_SIMPLE 4
1198 #define PICFLAG_SIDE_EFFECTS 8
1200 /* Given an initializer INIT, return the flag (PICFLAG_*) which better
1201 describe it. */
1203 static int
1204 picflag_from_initializer (tree init)
1206 if (init == error_mark_node)
1207 return PICFLAG_ERRONEOUS;
1208 else if (!TREE_CONSTANT (init))
1210 if (TREE_SIDE_EFFECTS (init))
1211 return PICFLAG_SIDE_EFFECTS;
1212 else
1213 return PICFLAG_NOT_ALL_CONSTANT;
1215 else if (!initializer_constant_valid_p (init, TREE_TYPE (init)))
1216 return PICFLAG_NOT_ALL_SIMPLE;
1217 return 0;
1220 /* Adjust INIT for going into a CONSTRUCTOR. */
1222 static tree
1223 massage_init_elt (tree type, tree init, tsubst_flags_t complain)
1225 init = digest_init_r (type, init, true, LOOKUP_IMPLICIT, complain);
1226 /* Strip a simple TARGET_EXPR when we know this is an initializer. */
1227 if (SIMPLE_TARGET_EXPR_P (init))
1228 init = TARGET_EXPR_INITIAL (init);
1229 /* When we defer constant folding within a statement, we may want to
1230 defer this folding as well. */
1231 tree t = fold_non_dependent_expr (init);
1232 t = maybe_constant_init (t);
1233 if (TREE_CONSTANT (t))
1234 init = t;
1235 return init;
1238 /* Subroutine of process_init_constructor, which will process an initializer
1239 INIT for an array or vector of type TYPE. Returns the flags (PICFLAG_*)
1240 which describe the initializers. */
1242 static int
1243 process_init_constructor_array (tree type, tree init,
1244 tsubst_flags_t complain)
1246 unsigned HOST_WIDE_INT i, len = 0;
1247 int flags = 0;
1248 bool unbounded = false;
1249 constructor_elt *ce;
1250 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (init);
1252 gcc_assert (TREE_CODE (type) == ARRAY_TYPE
1253 || VECTOR_TYPE_P (type));
1255 if (TREE_CODE (type) == ARRAY_TYPE)
1257 /* C++ flexible array members have a null domain. */
1258 tree domain = TYPE_DOMAIN (type);
1259 if (domain && TREE_CONSTANT (TYPE_MAX_VALUE (domain)))
1260 len = wi::ext (wi::to_offset (TYPE_MAX_VALUE (domain))
1261 - wi::to_offset (TYPE_MIN_VALUE (domain)) + 1,
1262 TYPE_PRECISION (TREE_TYPE (domain)),
1263 TYPE_SIGN (TREE_TYPE (domain))).to_uhwi ();
1264 else
1265 unbounded = true; /* Take as many as there are. */
1267 else
1268 /* Vectors are like simple fixed-size arrays. */
1269 len = TYPE_VECTOR_SUBPARTS (type);
1271 /* There must not be more initializers than needed. */
1272 if (!unbounded && vec_safe_length (v) > len)
1274 if (complain & tf_error)
1275 error ("too many initializers for %qT", type);
1276 else
1277 return PICFLAG_ERRONEOUS;
1280 FOR_EACH_VEC_SAFE_ELT (v, i, ce)
1282 if (ce->index)
1284 gcc_assert (TREE_CODE (ce->index) == INTEGER_CST);
1285 if (compare_tree_int (ce->index, i) != 0)
1287 ce->value = error_mark_node;
1288 sorry ("non-trivial designated initializers not supported");
1291 else
1292 ce->index = size_int (i);
1293 gcc_assert (ce->value);
1294 ce->value = massage_init_elt (TREE_TYPE (type), ce->value, complain);
1296 if (ce->value != error_mark_node)
1297 gcc_assert (same_type_ignoring_top_level_qualifiers_p
1298 (TREE_TYPE (type), TREE_TYPE (ce->value)));
1300 flags |= picflag_from_initializer (ce->value);
1303 /* No more initializers. If the array is unbounded, we are done. Otherwise,
1304 we must add initializers ourselves. */
1305 if (!unbounded)
1306 for (; i < len; ++i)
1308 tree next;
1310 if (type_build_ctor_call (TREE_TYPE (type)))
1312 /* If this type needs constructors run for default-initialization,
1313 we can't rely on the back end to do it for us, so make the
1314 initialization explicit by list-initializing from T{}. */
1315 next = build_constructor (init_list_type_node, NULL);
1316 next = massage_init_elt (TREE_TYPE (type), next, complain);
1317 if (initializer_zerop (next))
1318 /* The default zero-initialization is fine for us; don't
1319 add anything to the CONSTRUCTOR. */
1320 next = NULL_TREE;
1322 else if (!zero_init_p (TREE_TYPE (type)))
1323 next = build_zero_init (TREE_TYPE (type),
1324 /*nelts=*/NULL_TREE,
1325 /*static_storage_p=*/false);
1326 else
1327 /* The default zero-initialization is fine for us; don't
1328 add anything to the CONSTRUCTOR. */
1329 next = NULL_TREE;
1331 if (next)
1333 flags |= picflag_from_initializer (next);
1334 CONSTRUCTOR_APPEND_ELT (v, size_int (i), next);
1338 CONSTRUCTOR_ELTS (init) = v;
1339 return flags;
1342 /* Subroutine of process_init_constructor, which will process an initializer
1343 INIT for a class of type TYPE. Returns the flags (PICFLAG_*) which describe
1344 the initializers. */
1346 static int
1347 process_init_constructor_record (tree type, tree init,
1348 tsubst_flags_t complain)
1350 vec<constructor_elt, va_gc> *v = NULL;
1351 tree field;
1352 int skipped = 0;
1354 gcc_assert (TREE_CODE (type) == RECORD_TYPE);
1355 gcc_assert (!CLASSTYPE_VBASECLASSES (type));
1356 gcc_assert (!TYPE_BINFO (type)
1357 || cxx_dialect >= cxx1z
1358 || !BINFO_N_BASE_BINFOS (TYPE_BINFO (type)));
1359 gcc_assert (!TYPE_POLYMORPHIC_P (type));
1361 restart:
1362 int flags = 0;
1363 unsigned HOST_WIDE_INT idx = 0;
1364 /* Generally, we will always have an index for each initializer (which is
1365 a FIELD_DECL, put by reshape_init), but compound literals don't go trough
1366 reshape_init. So we need to handle both cases. */
1367 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
1369 tree next;
1370 tree type;
1372 if (!DECL_NAME (field) && DECL_C_BIT_FIELD (field))
1373 continue;
1375 if (TREE_CODE (field) != FIELD_DECL
1376 || (DECL_ARTIFICIAL (field)
1377 && !(cxx_dialect >= cxx1z && DECL_FIELD_IS_BASE (field))))
1378 continue;
1380 /* If this is a bitfield, first convert to the declared type. */
1381 type = TREE_TYPE (field);
1382 if (DECL_BIT_FIELD_TYPE (field))
1383 type = DECL_BIT_FIELD_TYPE (field);
1384 if (type == error_mark_node)
1385 return PICFLAG_ERRONEOUS;
1387 if (idx < CONSTRUCTOR_NELTS (init))
1389 constructor_elt *ce = &(*CONSTRUCTOR_ELTS (init))[idx];
1390 if (ce->index)
1392 /* We can have either a FIELD_DECL or an IDENTIFIER_NODE. The
1393 latter case can happen in templates where lookup has to be
1394 deferred. */
1395 gcc_assert (TREE_CODE (ce->index) == FIELD_DECL
1396 || identifier_p (ce->index));
1397 if (ce->index != field
1398 && ce->index != DECL_NAME (field))
1400 ce->value = error_mark_node;
1401 sorry ("non-trivial designated initializers not supported");
1405 gcc_assert (ce->value);
1406 next = massage_init_elt (type, ce->value, complain);
1407 ++idx;
1409 else if (DECL_INITIAL (field))
1411 if (skipped > 0)
1413 /* We're using an NSDMI past a field with implicit
1414 zero-init. Go back and make it explicit. */
1415 skipped = -1;
1416 vec_safe_truncate (v, 0);
1417 goto restart;
1419 /* C++14 aggregate NSDMI. */
1420 next = get_nsdmi (field, /*ctor*/false);
1422 else if (type_build_ctor_call (TREE_TYPE (field)))
1424 /* If this type needs constructors run for
1425 default-initialization, we can't rely on the back end to do it
1426 for us, so build up TARGET_EXPRs. If the type in question is
1427 a class, just build one up; if it's an array, recurse. */
1428 next = build_constructor (init_list_type_node, NULL);
1429 next = massage_init_elt (TREE_TYPE (field), next, complain);
1431 /* Warn when some struct elements are implicitly initialized. */
1432 if ((complain & tf_warning)
1433 && !EMPTY_CONSTRUCTOR_P (init))
1434 warning (OPT_Wmissing_field_initializers,
1435 "missing initializer for member %qD", field);
1437 else
1439 const_tree fldtype = TREE_TYPE (field);
1440 if (TREE_CODE (fldtype) == REFERENCE_TYPE)
1442 if (complain & tf_error)
1443 error ("member %qD is uninitialized reference", field);
1444 else
1445 return PICFLAG_ERRONEOUS;
1447 else if (CLASSTYPE_REF_FIELDS_NEED_INIT (fldtype))
1449 if (complain & tf_error)
1450 error ("member %qD with uninitialized reference fields", field);
1451 else
1452 return PICFLAG_ERRONEOUS;
1455 /* Warn when some struct elements are implicitly initialized
1456 to zero. However, avoid issuing the warning for flexible
1457 array members since they need not have any elements. */
1458 if ((TREE_CODE (fldtype) != ARRAY_TYPE || TYPE_DOMAIN (fldtype))
1459 && (complain & tf_warning)
1460 && !EMPTY_CONSTRUCTOR_P (init))
1461 warning (OPT_Wmissing_field_initializers,
1462 "missing initializer for member %qD", field);
1464 if (!zero_init_p (fldtype)
1465 || skipped < 0)
1466 next = build_zero_init (TREE_TYPE (field), /*nelts=*/NULL_TREE,
1467 /*static_storage_p=*/false);
1468 else
1470 /* The default zero-initialization is fine for us; don't
1471 add anything to the CONSTRUCTOR. */
1472 skipped = 1;
1473 continue;
1477 /* If this is a bitfield, now convert to the lowered type. */
1478 if (type != TREE_TYPE (field))
1479 next = cp_convert_and_check (TREE_TYPE (field), next, complain);
1480 flags |= picflag_from_initializer (next);
1481 CONSTRUCTOR_APPEND_ELT (v, field, next);
1484 if (idx < CONSTRUCTOR_NELTS (init))
1486 if (complain & tf_error)
1487 error ("too many initializers for %qT", type);
1488 else
1489 return PICFLAG_ERRONEOUS;
1492 CONSTRUCTOR_ELTS (init) = v;
1493 return flags;
1496 /* Subroutine of process_init_constructor, which will process a single
1497 initializer INIT for a union of type TYPE. Returns the flags (PICFLAG_*)
1498 which describe the initializer. */
1500 static int
1501 process_init_constructor_union (tree type, tree init,
1502 tsubst_flags_t complain)
1504 constructor_elt *ce;
1505 int len;
1507 /* If the initializer was empty, use the union's NSDMI if it has one.
1508 Otherwise use default zero initialization. */
1509 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
1511 for (tree field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1513 if (DECL_INITIAL (field))
1515 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (init),
1516 field,
1517 get_nsdmi (field, /*in_ctor=*/false));
1518 break;
1522 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
1523 return 0;
1526 len = CONSTRUCTOR_ELTS (init)->length ();
1527 if (len > 1)
1529 if (!(complain & tf_error))
1530 return PICFLAG_ERRONEOUS;
1531 error ("too many initializers for %qT", type);
1532 CONSTRUCTOR_ELTS (init)->block_remove (1, len-1);
1535 ce = &(*CONSTRUCTOR_ELTS (init))[0];
1537 /* If this element specifies a field, initialize via that field. */
1538 if (ce->index)
1540 if (TREE_CODE (ce->index) == FIELD_DECL)
1542 else if (identifier_p (ce->index))
1544 /* This can happen within a cast, see g++.dg/opt/cse2.C. */
1545 tree name = ce->index;
1546 tree field;
1547 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1548 if (DECL_NAME (field) == name)
1549 break;
1550 if (!field)
1552 if (complain & tf_error)
1553 error ("no field %qD found in union being initialized",
1554 field);
1555 ce->value = error_mark_node;
1557 ce->index = field;
1559 else
1561 gcc_assert (TREE_CODE (ce->index) == INTEGER_CST
1562 || TREE_CODE (ce->index) == RANGE_EXPR);
1563 if (complain & tf_error)
1564 error ("index value instead of field name in union initializer");
1565 ce->value = error_mark_node;
1568 else
1570 /* Find the first named field. ANSI decided in September 1990
1571 that only named fields count here. */
1572 tree field = TYPE_FIELDS (type);
1573 while (field && (!DECL_NAME (field) || TREE_CODE (field) != FIELD_DECL))
1574 field = TREE_CHAIN (field);
1575 if (field == NULL_TREE)
1577 if (complain & tf_error)
1578 error ("too many initializers for %qT", type);
1579 ce->value = error_mark_node;
1581 ce->index = field;
1584 if (ce->value && ce->value != error_mark_node)
1585 ce->value = massage_init_elt (TREE_TYPE (ce->index), ce->value, complain);
1587 return picflag_from_initializer (ce->value);
1590 /* Process INIT, a constructor for a variable of aggregate type TYPE. The
1591 constructor is a brace-enclosed initializer, and will be modified in-place.
1593 Each element is converted to the right type through digest_init, and
1594 missing initializers are added following the language rules (zero-padding,
1595 etc.).
1597 After the execution, the initializer will have TREE_CONSTANT if all elts are
1598 constant, and TREE_STATIC set if, in addition, all elts are simple enough
1599 constants that the assembler and linker can compute them.
1601 The function returns the initializer itself, or error_mark_node in case
1602 of error. */
1604 static tree
1605 process_init_constructor (tree type, tree init, tsubst_flags_t complain)
1607 int flags;
1609 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
1611 if (TREE_CODE (type) == ARRAY_TYPE || VECTOR_TYPE_P (type))
1612 flags = process_init_constructor_array (type, init, complain);
1613 else if (TREE_CODE (type) == RECORD_TYPE)
1614 flags = process_init_constructor_record (type, init, complain);
1615 else if (TREE_CODE (type) == UNION_TYPE)
1616 flags = process_init_constructor_union (type, init, complain);
1617 else
1618 gcc_unreachable ();
1620 if (flags & PICFLAG_ERRONEOUS)
1621 return error_mark_node;
1623 TREE_TYPE (init) = type;
1624 if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == NULL_TREE)
1625 cp_complete_array_type (&TREE_TYPE (init), init, /*do_default=*/0);
1626 if (flags & PICFLAG_SIDE_EFFECTS)
1628 TREE_CONSTANT (init) = false;
1629 TREE_SIDE_EFFECTS (init) = true;
1631 else if (flags & PICFLAG_NOT_ALL_CONSTANT)
1632 /* Make sure TREE_CONSTANT isn't set from build_constructor. */
1633 TREE_CONSTANT (init) = false;
1634 else
1636 TREE_CONSTANT (init) = 1;
1637 if (!(flags & PICFLAG_NOT_ALL_SIMPLE))
1638 TREE_STATIC (init) = 1;
1640 return init;
1643 /* Given a structure or union value DATUM, construct and return
1644 the structure or union component which results from narrowing
1645 that value to the base specified in BASETYPE. For example, given the
1646 hierarchy
1648 class L { int ii; };
1649 class A : L { ... };
1650 class B : L { ... };
1651 class C : A, B { ... };
1653 and the declaration
1655 C x;
1657 then the expression
1659 x.A::ii refers to the ii member of the L part of
1660 the A part of the C object named by X. In this case,
1661 DATUM would be x, and BASETYPE would be A.
1663 I used to think that this was nonconformant, that the standard specified
1664 that first we look up ii in A, then convert x to an L& and pull out the
1665 ii part. But in fact, it does say that we convert x to an A&; A here
1666 is known as the "naming class". (jason 2000-12-19)
1668 BINFO_P points to a variable initialized either to NULL_TREE or to the
1669 binfo for the specific base subobject we want to convert to. */
1671 tree
1672 build_scoped_ref (tree datum, tree basetype, tree* binfo_p)
1674 tree binfo;
1676 if (datum == error_mark_node)
1677 return error_mark_node;
1678 if (*binfo_p)
1679 binfo = *binfo_p;
1680 else
1681 binfo = lookup_base (TREE_TYPE (datum), basetype, ba_check,
1682 NULL, tf_warning_or_error);
1684 if (!binfo || binfo == error_mark_node)
1686 *binfo_p = NULL_TREE;
1687 if (!binfo)
1688 error_not_base_type (basetype, TREE_TYPE (datum));
1689 return error_mark_node;
1692 *binfo_p = binfo;
1693 return build_base_path (PLUS_EXPR, datum, binfo, 1,
1694 tf_warning_or_error);
1697 /* Build a reference to an object specified by the C++ `->' operator.
1698 Usually this just involves dereferencing the object, but if the
1699 `->' operator is overloaded, then such overloads must be
1700 performed until an object which does not have the `->' operator
1701 overloaded is found. An error is reported when circular pointer
1702 delegation is detected. */
1704 tree
1705 build_x_arrow (location_t loc, tree expr, tsubst_flags_t complain)
1707 tree orig_expr = expr;
1708 tree type = TREE_TYPE (expr);
1709 tree last_rval = NULL_TREE;
1710 vec<tree, va_gc> *types_memoized = NULL;
1712 if (type == error_mark_node)
1713 return error_mark_node;
1715 if (processing_template_decl)
1717 if (type && TREE_CODE (type) == POINTER_TYPE
1718 && !dependent_scope_p (TREE_TYPE (type)))
1719 /* Pointer to current instantiation, don't treat as dependent. */;
1720 else if (type_dependent_expression_p (expr))
1721 return build_min_nt_loc (loc, ARROW_EXPR, expr);
1722 expr = build_non_dependent_expr (expr);
1725 if (MAYBE_CLASS_TYPE_P (type))
1727 struct tinst_level *actual_inst = current_instantiation ();
1728 tree fn = NULL;
1730 while ((expr = build_new_op (loc, COMPONENT_REF,
1731 LOOKUP_NORMAL, expr, NULL_TREE, NULL_TREE,
1732 &fn, complain)))
1734 if (expr == error_mark_node)
1735 return error_mark_node;
1737 /* This provides a better instantiation backtrace in case of
1738 error. */
1739 if (fn && DECL_USE_TEMPLATE (fn))
1740 push_tinst_level_loc (fn,
1741 (current_instantiation () != actual_inst)
1742 ? DECL_SOURCE_LOCATION (fn)
1743 : input_location);
1744 fn = NULL;
1746 if (vec_member (TREE_TYPE (expr), types_memoized))
1748 if (complain & tf_error)
1749 error ("circular pointer delegation detected");
1750 return error_mark_node;
1753 vec_safe_push (types_memoized, TREE_TYPE (expr));
1754 last_rval = expr;
1757 while (current_instantiation () != actual_inst)
1758 pop_tinst_level ();
1760 if (last_rval == NULL_TREE)
1762 if (complain & tf_error)
1763 error ("base operand of %<->%> has non-pointer type %qT", type);
1764 return error_mark_node;
1767 if (TREE_CODE (TREE_TYPE (last_rval)) == REFERENCE_TYPE)
1768 last_rval = convert_from_reference (last_rval);
1770 else
1771 last_rval = decay_conversion (expr, complain);
1773 if (TYPE_PTR_P (TREE_TYPE (last_rval)))
1775 if (processing_template_decl)
1777 expr = build_min (ARROW_EXPR, TREE_TYPE (TREE_TYPE (last_rval)),
1778 orig_expr);
1779 TREE_SIDE_EFFECTS (expr) = TREE_SIDE_EFFECTS (last_rval);
1780 return expr;
1783 return cp_build_indirect_ref (last_rval, RO_NULL, complain);
1786 if (complain & tf_error)
1788 if (types_memoized)
1789 error ("result of %<operator->()%> yields non-pointer result");
1790 else
1791 error ("base operand of %<->%> is not a pointer");
1793 return error_mark_node;
1796 /* Return an expression for "DATUM .* COMPONENT". DATUM has not
1797 already been checked out to be of aggregate type. */
1799 tree
1800 build_m_component_ref (tree datum, tree component, tsubst_flags_t complain)
1802 tree ptrmem_type;
1803 tree objtype;
1804 tree type;
1805 tree binfo;
1806 tree ctype;
1808 if (error_operand_p (datum) || error_operand_p (component))
1809 return error_mark_node;
1811 datum = mark_lvalue_use (datum);
1812 component = mark_rvalue_use (component);
1814 ptrmem_type = TREE_TYPE (component);
1815 if (!TYPE_PTRMEM_P (ptrmem_type))
1817 if (complain & tf_error)
1818 error ("%qE cannot be used as a member pointer, since it is of "
1819 "type %qT", component, ptrmem_type);
1820 return error_mark_node;
1823 objtype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
1824 if (! MAYBE_CLASS_TYPE_P (objtype))
1826 if (complain & tf_error)
1827 error ("cannot apply member pointer %qE to %qE, which is of "
1828 "non-class type %qT", component, datum, objtype);
1829 return error_mark_node;
1832 type = TYPE_PTRMEM_POINTED_TO_TYPE (ptrmem_type);
1833 ctype = complete_type (TYPE_PTRMEM_CLASS_TYPE (ptrmem_type));
1835 if (!COMPLETE_TYPE_P (ctype))
1837 if (!same_type_p (ctype, objtype))
1838 goto mismatch;
1839 binfo = NULL;
1841 else
1843 binfo = lookup_base (objtype, ctype, ba_check, NULL, complain);
1845 if (!binfo)
1847 mismatch:
1848 if (complain & tf_error)
1849 error ("pointer to member type %qT incompatible with object "
1850 "type %qT", type, objtype);
1851 return error_mark_node;
1853 else if (binfo == error_mark_node)
1854 return error_mark_node;
1857 if (TYPE_PTRDATAMEM_P (ptrmem_type))
1859 cp_lvalue_kind kind = lvalue_kind (datum);
1860 tree ptype;
1862 /* Compute the type of the field, as described in [expr.ref].
1863 There's no such thing as a mutable pointer-to-member, so
1864 things are not as complex as they are for references to
1865 non-static data members. */
1866 type = cp_build_qualified_type (type,
1867 (cp_type_quals (type)
1868 | cp_type_quals (TREE_TYPE (datum))));
1870 datum = build_address (datum);
1872 /* Convert object to the correct base. */
1873 if (binfo)
1875 datum = build_base_path (PLUS_EXPR, datum, binfo, 1, complain);
1876 if (datum == error_mark_node)
1877 return error_mark_node;
1880 /* Build an expression for "object + offset" where offset is the
1881 value stored in the pointer-to-data-member. */
1882 ptype = build_pointer_type (type);
1883 datum = fold_build_pointer_plus (fold_convert (ptype, datum), component);
1884 datum = cp_build_indirect_ref (datum, RO_NULL, complain);
1885 if (datum == error_mark_node)
1886 return error_mark_node;
1888 /* If the object expression was an rvalue, return an rvalue. */
1889 if (kind & clk_class)
1890 datum = rvalue (datum);
1891 else if (kind & clk_rvalueref)
1892 datum = move (datum);
1893 return datum;
1895 else
1897 /* 5.5/6: In a .* expression whose object expression is an rvalue, the
1898 program is ill-formed if the second operand is a pointer to member
1899 function with ref-qualifier &. In a .* expression whose object
1900 expression is an lvalue, the program is ill-formed if the second
1901 operand is a pointer to member function with ref-qualifier &&. */
1902 if (FUNCTION_REF_QUALIFIED (type))
1904 bool lval = lvalue_p (datum);
1905 if (lval && FUNCTION_RVALUE_QUALIFIED (type))
1907 if (complain & tf_error)
1908 error ("pointer-to-member-function type %qT requires an rvalue",
1909 ptrmem_type);
1910 return error_mark_node;
1912 else if (!lval && !FUNCTION_RVALUE_QUALIFIED (type))
1914 if (complain & tf_error)
1915 error ("pointer-to-member-function type %qT requires an lvalue",
1916 ptrmem_type);
1917 return error_mark_node;
1920 return build2 (OFFSET_REF, type, datum, component);
1924 /* Return a tree node for the expression TYPENAME '(' PARMS ')'. */
1926 tree
1927 build_functional_cast (tree exp, tree parms, tsubst_flags_t complain)
1929 /* This is either a call to a constructor,
1930 or a C cast in C++'s `functional' notation. */
1932 /* The type to which we are casting. */
1933 tree type;
1934 vec<tree, va_gc> *parmvec;
1936 if (error_operand_p (exp) || parms == error_mark_node)
1937 return error_mark_node;
1939 if (TREE_CODE (exp) == TYPE_DECL)
1941 type = TREE_TYPE (exp);
1943 if (complain & tf_warning
1944 && TREE_DEPRECATED (type)
1945 && DECL_ARTIFICIAL (exp))
1946 warn_deprecated_use (type, NULL_TREE);
1948 else
1949 type = exp;
1951 /* We need to check this explicitly, since value-initialization of
1952 arrays is allowed in other situations. */
1953 if (TREE_CODE (type) == ARRAY_TYPE)
1955 if (complain & tf_error)
1956 error ("functional cast to array type %qT", type);
1957 return error_mark_node;
1960 if (tree anode = type_uses_auto (type))
1962 if (!CLASS_PLACEHOLDER_TEMPLATE (anode))
1964 if (complain & tf_error)
1965 error ("invalid use of %qT", anode);
1966 return error_mark_node;
1968 else if (!parms)
1970 if (complain & tf_error)
1971 error ("cannot deduce template arguments for %qT from ()", anode);
1972 return error_mark_node;
1974 else
1975 type = do_auto_deduction (type, parms, anode, complain,
1976 adc_variable_type);
1979 if (processing_template_decl)
1981 tree t;
1983 /* Diagnose this even in a template. We could also try harder
1984 to give all the usual errors when the type and args are
1985 non-dependent... */
1986 if (TREE_CODE (type) == REFERENCE_TYPE && !parms)
1988 if (complain & tf_error)
1989 error ("invalid value-initialization of reference type");
1990 return error_mark_node;
1993 t = build_min (CAST_EXPR, type, parms);
1994 /* We don't know if it will or will not have side effects. */
1995 TREE_SIDE_EFFECTS (t) = 1;
1996 return t;
1999 if (! MAYBE_CLASS_TYPE_P (type))
2001 if (parms == NULL_TREE)
2003 if (VOID_TYPE_P (type))
2004 return void_node;
2005 return build_value_init (cv_unqualified (type), complain);
2008 /* This must build a C cast. */
2009 parms = build_x_compound_expr_from_list (parms, ELK_FUNC_CAST, complain);
2010 return cp_build_c_cast (type, parms, complain);
2013 /* Prepare to evaluate as a call to a constructor. If this expression
2014 is actually used, for example,
2016 return X (arg1, arg2, ...);
2018 then the slot being initialized will be filled in. */
2020 if (!complete_type_or_maybe_complain (type, NULL_TREE, complain))
2021 return error_mark_node;
2022 if (abstract_virtuals_error_sfinae (ACU_CAST, type, complain))
2023 return error_mark_node;
2025 /* [expr.type.conv]
2027 If the expression list is a single-expression, the type
2028 conversion is equivalent (in definedness, and if defined in
2029 meaning) to the corresponding cast expression. */
2030 if (parms && TREE_CHAIN (parms) == NULL_TREE)
2031 return cp_build_c_cast (type, TREE_VALUE (parms), complain);
2033 /* [expr.type.conv]
2035 The expression T(), where T is a simple-type-specifier for a
2036 non-array complete object type or the (possibly cv-qualified)
2037 void type, creates an rvalue of the specified type, which is
2038 value-initialized. */
2040 if (parms == NULL_TREE)
2042 exp = build_value_init (type, complain);
2043 exp = get_target_expr_sfinae (exp, complain);
2044 return exp;
2047 /* Call the constructor. */
2048 parmvec = make_tree_vector ();
2049 for (; parms != NULL_TREE; parms = TREE_CHAIN (parms))
2050 vec_safe_push (parmvec, TREE_VALUE (parms));
2051 exp = build_special_member_call (NULL_TREE, complete_ctor_identifier,
2052 &parmvec, type, LOOKUP_NORMAL, complain);
2053 release_tree_vector (parmvec);
2055 if (exp == error_mark_node)
2056 return error_mark_node;
2058 return build_cplus_new (type, exp, complain);
2062 /* Add new exception specifier SPEC, to the LIST we currently have.
2063 If it's already in LIST then do nothing.
2064 Moan if it's bad and we're allowed to. COMPLAIN < 0 means we
2065 know what we're doing. */
2067 tree
2068 add_exception_specifier (tree list, tree spec, int complain)
2070 bool ok;
2071 tree core = spec;
2072 bool is_ptr;
2073 diagnostic_t diag_type = DK_UNSPECIFIED; /* none */
2075 if (spec == error_mark_node)
2076 return list;
2078 gcc_assert (spec && (!list || TREE_VALUE (list)));
2080 /* [except.spec] 1, type in an exception specifier shall not be
2081 incomplete, or pointer or ref to incomplete other than pointer
2082 to cv void. */
2083 is_ptr = TYPE_PTR_P (core);
2084 if (is_ptr || TREE_CODE (core) == REFERENCE_TYPE)
2085 core = TREE_TYPE (core);
2086 if (complain < 0)
2087 ok = true;
2088 else if (VOID_TYPE_P (core))
2089 ok = is_ptr;
2090 else if (TREE_CODE (core) == TEMPLATE_TYPE_PARM)
2091 ok = true;
2092 else if (processing_template_decl)
2093 ok = true;
2094 else
2096 ok = true;
2097 /* 15.4/1 says that types in an exception specifier must be complete,
2098 but it seems more reasonable to only require this on definitions
2099 and calls. So just give a pedwarn at this point; we will give an
2100 error later if we hit one of those two cases. */
2101 if (!COMPLETE_TYPE_P (complete_type (core)))
2102 diag_type = DK_PEDWARN; /* pedwarn */
2105 if (ok)
2107 tree probe;
2109 for (probe = list; probe; probe = TREE_CHAIN (probe))
2110 if (same_type_p (TREE_VALUE (probe), spec))
2111 break;
2112 if (!probe)
2113 list = tree_cons (NULL_TREE, spec, list);
2115 else
2116 diag_type = DK_ERROR; /* error */
2118 if (diag_type != DK_UNSPECIFIED
2119 && (complain & tf_warning_or_error))
2120 cxx_incomplete_type_diagnostic (NULL_TREE, core, diag_type);
2122 return list;
2125 /* Like nothrow_spec_p, but don't abort on deferred noexcept. */
2127 static bool
2128 nothrow_spec_p_uninst (const_tree spec)
2130 if (DEFERRED_NOEXCEPT_SPEC_P (spec))
2131 return false;
2132 return nothrow_spec_p (spec);
2135 /* Combine the two exceptions specifier lists LIST and ADD, and return
2136 their union. */
2138 tree
2139 merge_exception_specifiers (tree list, tree add)
2141 tree noex, orig_list;
2143 /* No exception-specifier or noexcept(false) are less strict than
2144 anything else. Prefer the newer variant (LIST). */
2145 if (!list || list == noexcept_false_spec)
2146 return list;
2147 else if (!add || add == noexcept_false_spec)
2148 return add;
2150 /* noexcept(true) and throw() are stricter than anything else.
2151 As above, prefer the more recent one (LIST). */
2152 if (nothrow_spec_p_uninst (add))
2153 return list;
2155 /* Two implicit noexcept specs (e.g. on a destructor) are equivalent. */
2156 if (UNEVALUATED_NOEXCEPT_SPEC_P (add)
2157 && UNEVALUATED_NOEXCEPT_SPEC_P (list))
2158 return list;
2159 /* We should have instantiated other deferred noexcept specs by now. */
2160 gcc_assert (!DEFERRED_NOEXCEPT_SPEC_P (add));
2162 if (nothrow_spec_p_uninst (list))
2163 return add;
2164 noex = TREE_PURPOSE (list);
2165 gcc_checking_assert (!TREE_PURPOSE (add)
2166 || errorcount || !flag_exceptions
2167 || cp_tree_equal (noex, TREE_PURPOSE (add)));
2169 /* Combine the dynamic-exception-specifiers, if any. */
2170 orig_list = list;
2171 for (; add && TREE_VALUE (add); add = TREE_CHAIN (add))
2173 tree spec = TREE_VALUE (add);
2174 tree probe;
2176 for (probe = orig_list; probe && TREE_VALUE (probe);
2177 probe = TREE_CHAIN (probe))
2178 if (same_type_p (TREE_VALUE (probe), spec))
2179 break;
2180 if (!probe)
2182 spec = build_tree_list (NULL_TREE, spec);
2183 TREE_CHAIN (spec) = list;
2184 list = spec;
2188 /* Keep the noexcept-specifier at the beginning of the list. */
2189 if (noex != TREE_PURPOSE (list))
2190 list = tree_cons (noex, TREE_VALUE (list), TREE_CHAIN (list));
2192 return list;
2195 /* Subroutine of build_call. Ensure that each of the types in the
2196 exception specification is complete. Technically, 15.4/1 says that
2197 they need to be complete when we see a declaration of the function,
2198 but we should be able to get away with only requiring this when the
2199 function is defined or called. See also add_exception_specifier. */
2201 void
2202 require_complete_eh_spec_types (tree fntype, tree decl)
2204 tree raises;
2205 /* Don't complain about calls to op new. */
2206 if (decl && DECL_ARTIFICIAL (decl))
2207 return;
2208 for (raises = TYPE_RAISES_EXCEPTIONS (fntype); raises;
2209 raises = TREE_CHAIN (raises))
2211 tree type = TREE_VALUE (raises);
2212 if (type && !COMPLETE_TYPE_P (type))
2214 if (decl)
2215 error
2216 ("call to function %qD which throws incomplete type %q#T",
2217 decl, type);
2218 else
2219 error ("call to function which throws incomplete type %q#T",
2220 decl);
2226 #include "gt-cp-typeck2.h"