[C++ PATCH] refactor duplicate_decls
[official-gcc.git] / gcc / cp / typeck2.c
blobfec1db00ca4c5588155e2e4e52534c3baf52449a
1 /* Report error messages, build initializers, and perform
2 some front-end optimizations for C++ compiler.
3 Copyright (C) 1987-2018 Free Software Foundation, Inc.
4 Hacked by Michael Tiemann (tiemann@cygnus.com)
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
23 /* This file is part of the C++ front end.
24 It contains routines to build C++ expressions given their operands,
25 including computing the types of the result, C and C++ specific error
26 checks, and some optimization. */
28 #include "config.h"
29 #include "system.h"
30 #include "coretypes.h"
31 #include "cp-tree.h"
32 #include "stor-layout.h"
33 #include "varasm.h"
34 #include "intl.h"
36 static tree
37 process_init_constructor (tree type, tree init, int nested,
38 tsubst_flags_t complain);
41 /* Print an error message stemming from an attempt to use
42 BASETYPE as a base class for TYPE. */
44 tree
45 error_not_base_type (tree basetype, tree type)
47 if (TREE_CODE (basetype) == FUNCTION_DECL)
48 basetype = DECL_CONTEXT (basetype);
49 error ("type %qT is not a base type for type %qT", basetype, type);
50 return error_mark_node;
53 tree
54 binfo_or_else (tree base, tree type)
56 tree binfo = lookup_base (type, base, ba_unique,
57 NULL, tf_warning_or_error);
59 if (binfo == error_mark_node)
60 return NULL_TREE;
61 else if (!binfo)
62 error_not_base_type (base, type);
63 return binfo;
66 /* According to ARM $7.1.6, "A `const' object may be initialized, but its
67 value may not be changed thereafter. */
69 void
70 cxx_readonly_error (location_t loc, tree arg, enum lvalue_use errstring)
73 /* This macro is used to emit diagnostics to ensure that all format
74 strings are complete sentences, visible to gettext and checked at
75 compile time. */
77 #define ERROR_FOR_ASSIGNMENT(LOC, AS, ASM, IN, DE, ARG) \
78 do { \
79 switch (errstring) \
80 { \
81 case lv_assign: \
82 error_at (LOC, AS, ARG); \
83 break; \
84 case lv_asm: \
85 error_at (LOC, ASM, ARG); \
86 break; \
87 case lv_increment: \
88 error_at (LOC, IN, ARG); \
89 break; \
90 case lv_decrement: \
91 error_at (LOC, DE, ARG); \
92 break; \
93 default: \
94 gcc_unreachable (); \
95 } \
96 } while (0)
98 /* Handle C++-specific things first. */
100 if (VAR_P (arg)
101 && DECL_LANG_SPECIFIC (arg)
102 && DECL_IN_AGGR_P (arg)
103 && !TREE_STATIC (arg))
104 ERROR_FOR_ASSIGNMENT (loc,
105 G_("assignment of constant field %qD"),
106 G_("constant field %qD used as %<asm%> output"),
107 G_("increment of constant field %qD"),
108 G_("decrement of constant field %qD"),
109 arg);
110 else if (INDIRECT_REF_P (arg)
111 && TYPE_REF_P (TREE_TYPE (TREE_OPERAND (arg, 0)))
112 && (VAR_P (TREE_OPERAND (arg, 0))
113 || TREE_CODE (TREE_OPERAND (arg, 0)) == PARM_DECL))
114 ERROR_FOR_ASSIGNMENT (loc,
115 G_("assignment of read-only reference %qD"),
116 G_("read-only reference %qD used as %<asm%> output"),
117 G_("increment of read-only reference %qD"),
118 G_("decrement of read-only reference %qD"),
119 TREE_OPERAND (arg, 0));
120 else
121 readonly_error (loc, arg, errstring);
124 /* Structure that holds information about declarations whose type was
125 incomplete and we could not check whether it was abstract or not. */
127 struct GTY((chain_next ("%h.next"), for_user)) pending_abstract_type {
128 /* Declaration which we are checking for abstractness. It is either
129 a DECL node, or an IDENTIFIER_NODE if we do not have a full
130 declaration available. */
131 tree decl;
133 /* Type which will be checked for abstractness. */
134 tree type;
136 /* Kind of use in an unnamed declarator. */
137 enum abstract_class_use use;
139 /* Position of the declaration. This is only needed for IDENTIFIER_NODEs,
140 because DECLs already carry locus information. */
141 location_t locus;
143 /* Link to the next element in list. */
144 struct pending_abstract_type* next;
147 struct abstract_type_hasher : ggc_ptr_hash<pending_abstract_type>
149 typedef tree compare_type;
150 static hashval_t hash (pending_abstract_type *);
151 static bool equal (pending_abstract_type *, tree);
154 /* Compute the hash value of the node VAL. This function is used by the
155 hash table abstract_pending_vars. */
157 hashval_t
158 abstract_type_hasher::hash (pending_abstract_type *pat)
160 return (hashval_t) TYPE_UID (pat->type);
164 /* Compare node VAL1 with the type VAL2. This function is used by the
165 hash table abstract_pending_vars. */
167 bool
168 abstract_type_hasher::equal (pending_abstract_type *pat1, tree type2)
170 return (pat1->type == type2);
173 /* Hash table that maintains pending_abstract_type nodes, for which we still
174 need to check for type abstractness. The key of the table is the type
175 of the declaration. */
176 static GTY (()) hash_table<abstract_type_hasher> *abstract_pending_vars = NULL;
178 static int abstract_virtuals_error_sfinae (tree, tree, abstract_class_use, tsubst_flags_t);
180 /* This function is called after TYPE is completed, and will check if there
181 are pending declarations for which we still need to verify the abstractness
182 of TYPE, and emit a diagnostic (through abstract_virtuals_error) if TYPE
183 turned out to be incomplete. */
185 void
186 complete_type_check_abstract (tree type)
188 struct pending_abstract_type *pat;
189 location_t cur_loc = input_location;
191 gcc_assert (COMPLETE_TYPE_P (type));
193 if (!abstract_pending_vars)
194 return;
196 /* Retrieve the list of pending declarations for this type. */
197 pending_abstract_type **slot
198 = abstract_pending_vars->find_slot_with_hash (type, TYPE_UID (type),
199 NO_INSERT);
200 if (!slot)
201 return;
202 pat = *slot;
203 gcc_assert (pat);
205 /* If the type is not abstract, do not do anything. */
206 if (CLASSTYPE_PURE_VIRTUALS (type))
208 struct pending_abstract_type *prev = 0, *next;
210 /* Reverse the list to emit the errors in top-down order. */
211 for (; pat; pat = next)
213 next = pat->next;
214 pat->next = prev;
215 prev = pat;
217 pat = prev;
219 /* Go through the list, and call abstract_virtuals_error for each
220 element: it will issue a diagnostic if the type is abstract. */
221 while (pat)
223 gcc_assert (type == pat->type);
225 /* Tweak input_location so that the diagnostic appears at the correct
226 location. Notice that this is only needed if the decl is an
227 IDENTIFIER_NODE. */
228 input_location = pat->locus;
229 abstract_virtuals_error_sfinae (pat->decl, pat->type, pat->use,
230 tf_warning_or_error);
231 pat = pat->next;
235 abstract_pending_vars->clear_slot (slot);
237 input_location = cur_loc;
241 /* If TYPE has abstract virtual functions, issue an error about trying
242 to create an object of that type. DECL is the object declared, or
243 NULL_TREE if the declaration is unavailable, in which case USE specifies
244 the kind of invalid use. Returns 1 if an error occurred; zero if
245 all was well. */
247 static int
248 abstract_virtuals_error_sfinae (tree decl, tree type, abstract_class_use use,
249 tsubst_flags_t complain)
251 vec<tree, va_gc> *pure;
253 /* This function applies only to classes. Any other entity can never
254 be abstract. */
255 if (!CLASS_TYPE_P (type))
256 return 0;
257 type = TYPE_MAIN_VARIANT (type);
259 #if 0
260 /* Instantiation here seems to be required by the standard,
261 but breaks e.g. boost::bind. FIXME! */
262 /* In SFINAE, non-N3276 context, force instantiation. */
263 if (!(complain & (tf_error|tf_decltype)))
264 complete_type (type);
265 #endif
267 /* If the type is incomplete, we register it within a hash table,
268 so that we can check again once it is completed. This makes sense
269 only for objects for which we have a declaration or at least a
270 name. */
271 if (!COMPLETE_TYPE_P (type) && (complain & tf_error))
273 struct pending_abstract_type *pat;
275 gcc_assert (!decl || DECL_P (decl) || identifier_p (decl));
277 if (!abstract_pending_vars)
278 abstract_pending_vars
279 = hash_table<abstract_type_hasher>::create_ggc (31);
281 pending_abstract_type **slot
282 = abstract_pending_vars->find_slot_with_hash (type, TYPE_UID (type),
283 INSERT);
285 pat = ggc_alloc<pending_abstract_type> ();
286 pat->type = type;
287 pat->decl = decl;
288 pat->use = use;
289 pat->locus = ((decl && DECL_P (decl))
290 ? DECL_SOURCE_LOCATION (decl)
291 : input_location);
293 pat->next = *slot;
294 *slot = pat;
296 return 0;
299 if (!TYPE_SIZE (type))
300 /* TYPE is being defined, and during that time
301 CLASSTYPE_PURE_VIRTUALS holds the inline friends. */
302 return 0;
304 pure = CLASSTYPE_PURE_VIRTUALS (type);
305 if (!pure)
306 return 0;
308 if (!(complain & tf_error))
309 return 1;
311 auto_diagnostic_group d;
312 if (decl)
314 if (VAR_P (decl))
315 error ("cannot declare variable %q+D to be of abstract "
316 "type %qT", decl, type);
317 else if (TREE_CODE (decl) == PARM_DECL)
319 if (DECL_NAME (decl))
320 error ("cannot declare parameter %q+D to be of abstract type %qT",
321 decl, type);
322 else
323 error ("cannot declare parameter to be of abstract type %qT",
324 type);
326 else if (TREE_CODE (decl) == FIELD_DECL)
327 error ("cannot declare field %q+D to be of abstract type %qT",
328 decl, type);
329 else if (TREE_CODE (decl) == FUNCTION_DECL
330 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
331 error ("invalid abstract return type for member function %q+#D", decl);
332 else if (TREE_CODE (decl) == FUNCTION_DECL)
333 error ("invalid abstract return type for function %q+#D", decl);
334 else if (identifier_p (decl))
335 /* Here we do not have location information. */
336 error ("invalid abstract type %qT for %qE", type, decl);
337 else
338 error ("invalid abstract type for %q+D", decl);
340 else switch (use)
342 case ACU_ARRAY:
343 error ("creating array of %qT, which is an abstract class type", type);
344 break;
345 case ACU_CAST:
346 error ("invalid cast to abstract class type %qT", type);
347 break;
348 case ACU_NEW:
349 error ("invalid new-expression of abstract class type %qT", type);
350 break;
351 case ACU_RETURN:
352 error ("invalid abstract return type %qT", type);
353 break;
354 case ACU_PARM:
355 error ("invalid abstract parameter type %qT", type);
356 break;
357 case ACU_THROW:
358 error ("expression of abstract class type %qT cannot "
359 "be used in throw-expression", type);
360 break;
361 case ACU_CATCH:
362 error ("cannot declare catch parameter to be of abstract "
363 "class type %qT", type);
364 break;
365 default:
366 error ("cannot allocate an object of abstract type %qT", type);
369 /* Only go through this once. */
370 if (pure->length ())
372 unsigned ix;
373 tree fn;
375 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
376 " because the following virtual functions are pure within %qT:",
377 type);
379 FOR_EACH_VEC_ELT (*pure, ix, fn)
380 if (! DECL_CLONED_FUNCTION_P (fn)
381 || DECL_COMPLETE_DESTRUCTOR_P (fn))
382 inform (DECL_SOURCE_LOCATION (fn), "\t%#qD", fn);
384 /* Now truncate the vector. This leaves it non-null, so we know
385 there are pure virtuals, but empty so we don't list them out
386 again. */
387 pure->truncate (0);
390 return 1;
394 abstract_virtuals_error_sfinae (tree decl, tree type, tsubst_flags_t complain)
396 return abstract_virtuals_error_sfinae (decl, type, ACU_UNKNOWN, complain);
400 abstract_virtuals_error_sfinae (abstract_class_use use, tree type,
401 tsubst_flags_t complain)
403 return abstract_virtuals_error_sfinae (NULL_TREE, type, use, complain);
407 /* Wrapper for the above function in the common case of wanting errors. */
410 abstract_virtuals_error (tree decl, tree type)
412 return abstract_virtuals_error_sfinae (decl, type, tf_warning_or_error);
416 abstract_virtuals_error (abstract_class_use use, tree type)
418 return abstract_virtuals_error_sfinae (use, type, tf_warning_or_error);
421 /* Print an inform about the declaration of the incomplete type TYPE. */
423 void
424 cxx_incomplete_type_inform (const_tree type)
426 if (!TYPE_MAIN_DECL (type))
427 return;
429 location_t loc = DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type));
430 tree ptype = strip_top_quals (CONST_CAST_TREE (type));
432 if (current_class_type
433 && TYPE_BEING_DEFINED (current_class_type)
434 && same_type_p (ptype, current_class_type))
435 inform (loc, "definition of %q#T is not complete until "
436 "the closing brace", ptype);
437 else if (!TYPE_TEMPLATE_INFO (ptype))
438 inform (loc, "forward declaration of %q#T", ptype);
439 else
440 inform (loc, "declaration of %q#T", ptype);
443 /* Print an error message for invalid use of an incomplete type.
444 VALUE is the expression that was used (or 0 if that isn't known)
445 and TYPE is the type that was invalid. DIAG_KIND indicates the
446 type of diagnostic (see diagnostic.def). */
448 void
449 cxx_incomplete_type_diagnostic (location_t loc, const_tree value,
450 const_tree type, diagnostic_t diag_kind)
452 bool is_decl = false, complained = false;
454 gcc_assert (diag_kind == DK_WARNING
455 || diag_kind == DK_PEDWARN
456 || diag_kind == DK_ERROR);
458 /* Avoid duplicate error message. */
459 if (TREE_CODE (type) == ERROR_MARK)
460 return;
462 if (value != 0 && (VAR_P (value)
463 || TREE_CODE (value) == PARM_DECL
464 || TREE_CODE (value) == FIELD_DECL))
466 complained = emit_diagnostic (diag_kind, DECL_SOURCE_LOCATION (value), 0,
467 "%qD has incomplete type", value);
468 is_decl = true;
470 retry:
471 /* We must print an error message. Be clever about what it says. */
473 switch (TREE_CODE (type))
475 case RECORD_TYPE:
476 case UNION_TYPE:
477 case ENUMERAL_TYPE:
478 if (!is_decl)
479 complained = emit_diagnostic (diag_kind, loc, 0,
480 "invalid use of incomplete type %q#T",
481 type);
482 if (complained)
483 cxx_incomplete_type_inform (type);
484 break;
486 case VOID_TYPE:
487 emit_diagnostic (diag_kind, loc, 0,
488 "invalid use of %qT", type);
489 break;
491 case ARRAY_TYPE:
492 if (TYPE_DOMAIN (type))
494 type = TREE_TYPE (type);
495 goto retry;
497 emit_diagnostic (diag_kind, loc, 0,
498 "invalid use of array with unspecified bounds");
499 break;
501 case OFFSET_TYPE:
502 bad_member:
504 tree member = TREE_OPERAND (value, 1);
505 if (is_overloaded_fn (member))
506 member = get_first_fn (member);
508 if (DECL_FUNCTION_MEMBER_P (member)
509 && ! flag_ms_extensions)
510 emit_diagnostic (diag_kind, loc, 0,
511 "invalid use of member function %qD "
512 "(did you forget the %<()%> ?)", member);
513 else
514 emit_diagnostic (diag_kind, loc, 0,
515 "invalid use of member %qD "
516 "(did you forget the %<&%> ?)", member);
518 break;
520 case TEMPLATE_TYPE_PARM:
521 if (is_auto (type))
523 if (CLASS_PLACEHOLDER_TEMPLATE (type))
524 emit_diagnostic (diag_kind, loc, 0,
525 "invalid use of placeholder %qT", type);
526 else
527 emit_diagnostic (diag_kind, loc, 0,
528 "invalid use of %qT", type);
530 else
531 emit_diagnostic (diag_kind, loc, 0,
532 "invalid use of template type parameter %qT", type);
533 break;
535 case BOUND_TEMPLATE_TEMPLATE_PARM:
536 emit_diagnostic (diag_kind, loc, 0,
537 "invalid use of template template parameter %qT",
538 TYPE_NAME (type));
539 break;
541 case TYPENAME_TYPE:
542 case DECLTYPE_TYPE:
543 emit_diagnostic (diag_kind, loc, 0,
544 "invalid use of dependent type %qT", type);
545 break;
547 case LANG_TYPE:
548 if (type == init_list_type_node)
550 emit_diagnostic (diag_kind, loc, 0,
551 "invalid use of brace-enclosed initializer list");
552 break;
554 gcc_assert (type == unknown_type_node);
555 if (value && TREE_CODE (value) == COMPONENT_REF)
556 goto bad_member;
557 else if (value && TREE_CODE (value) == ADDR_EXPR)
558 emit_diagnostic (diag_kind, loc, 0,
559 "address of overloaded function with no contextual "
560 "type information");
561 else if (value && TREE_CODE (value) == OVERLOAD)
562 emit_diagnostic (diag_kind, loc, 0,
563 "overloaded function with no contextual type information");
564 else
565 emit_diagnostic (diag_kind, loc, 0,
566 "insufficient contextual information to determine type");
567 break;
569 default:
570 gcc_unreachable ();
574 /* Print an error message for invalid use of an incomplete type.
575 VALUE is the expression that was used (or 0 if that isn't known)
576 and TYPE is the type that was invalid. */
578 void
579 cxx_incomplete_type_error (location_t loc, const_tree value, const_tree type)
581 cxx_incomplete_type_diagnostic (loc, value, type, DK_ERROR);
585 /* The recursive part of split_nonconstant_init. DEST is an lvalue
586 expression to which INIT should be assigned. INIT is a CONSTRUCTOR.
587 Return true if the whole of the value was initialized by the
588 generated statements. */
590 static bool
591 split_nonconstant_init_1 (tree dest, tree init)
593 unsigned HOST_WIDE_INT idx;
594 tree field_index, value;
595 tree type = TREE_TYPE (dest);
596 tree inner_type = NULL;
597 bool array_type_p = false;
598 bool complete_p = true;
599 HOST_WIDE_INT num_split_elts = 0;
601 switch (TREE_CODE (type))
603 case ARRAY_TYPE:
604 inner_type = TREE_TYPE (type);
605 array_type_p = true;
606 if ((TREE_SIDE_EFFECTS (init)
607 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
608 || vla_type_p (type))
610 /* For an array, we only need/want a single cleanup region rather
611 than one per element. */
612 tree code = build_vec_init (dest, NULL_TREE, init, false, 1,
613 tf_warning_or_error);
614 add_stmt (code);
615 return true;
617 /* FALLTHRU */
619 case RECORD_TYPE:
620 case UNION_TYPE:
621 case QUAL_UNION_TYPE:
622 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), idx,
623 field_index, value)
625 /* The current implementation of this algorithm assumes that
626 the field was set for all the elements. This is usually done
627 by process_init_constructor. */
628 gcc_assert (field_index);
630 if (!array_type_p)
631 inner_type = TREE_TYPE (field_index);
633 if (TREE_CODE (value) == CONSTRUCTOR)
635 tree sub;
637 if (array_type_p)
638 sub = build4 (ARRAY_REF, inner_type, dest, field_index,
639 NULL_TREE, NULL_TREE);
640 else
641 sub = build3 (COMPONENT_REF, inner_type, dest, field_index,
642 NULL_TREE);
644 if (!split_nonconstant_init_1 (sub, value))
645 complete_p = false;
646 else
647 CONSTRUCTOR_ELTS (init)->ordered_remove (idx--);
648 num_split_elts++;
650 else if (!initializer_constant_valid_p (value, inner_type))
652 tree code;
653 tree sub;
655 /* FIXME: Ordered removal is O(1) so the whole function is
656 worst-case quadratic. This could be fixed using an aside
657 bitmap to record which elements must be removed and remove
658 them all at the same time. Or by merging
659 split_non_constant_init into process_init_constructor_array,
660 that is separating constants from non-constants while building
661 the vector. */
662 CONSTRUCTOR_ELTS (init)->ordered_remove (idx);
663 --idx;
665 if (TREE_CODE (field_index) == RANGE_EXPR)
667 /* Use build_vec_init to initialize a range. */
668 tree low = TREE_OPERAND (field_index, 0);
669 tree hi = TREE_OPERAND (field_index, 1);
670 sub = build4 (ARRAY_REF, inner_type, dest, low,
671 NULL_TREE, NULL_TREE);
672 sub = cp_build_addr_expr (sub, tf_warning_or_error);
673 tree max = size_binop (MINUS_EXPR, hi, low);
674 code = build_vec_init (sub, max, value, false, 0,
675 tf_warning_or_error);
676 add_stmt (code);
677 if (tree_fits_shwi_p (max))
678 num_split_elts += tree_to_shwi (max);
680 else
682 if (array_type_p)
683 sub = build4 (ARRAY_REF, inner_type, dest, field_index,
684 NULL_TREE, NULL_TREE);
685 else
686 sub = build3 (COMPONENT_REF, inner_type, dest, field_index,
687 NULL_TREE);
689 code = build2 (INIT_EXPR, inner_type, sub, value);
690 code = build_stmt (input_location, EXPR_STMT, code);
691 code = maybe_cleanup_point_expr_void (code);
692 add_stmt (code);
693 if (tree cleanup
694 = cxx_maybe_build_cleanup (sub, tf_warning_or_error))
695 finish_eh_cleanup (cleanup);
698 num_split_elts++;
701 break;
703 case VECTOR_TYPE:
704 if (!initializer_constant_valid_p (init, type))
706 tree code;
707 tree cons = copy_node (init);
708 CONSTRUCTOR_ELTS (init) = NULL;
709 code = build2 (MODIFY_EXPR, type, dest, cons);
710 code = build_stmt (input_location, EXPR_STMT, code);
711 add_stmt (code);
712 num_split_elts += CONSTRUCTOR_NELTS (init);
714 break;
716 default:
717 gcc_unreachable ();
720 /* The rest of the initializer is now a constant. */
721 TREE_CONSTANT (init) = 1;
723 /* We didn't split out anything. */
724 if (num_split_elts == 0)
725 return false;
727 return complete_p && complete_ctor_at_level_p (TREE_TYPE (init),
728 num_split_elts, inner_type);
731 /* A subroutine of store_init_value. Splits non-constant static
732 initializer INIT into a constant part and generates code to
733 perform the non-constant part of the initialization to DEST.
734 Returns the code for the runtime init. */
736 tree
737 split_nonconstant_init (tree dest, tree init)
739 tree code;
741 if (TREE_CODE (init) == TARGET_EXPR)
742 init = TARGET_EXPR_INITIAL (init);
743 if (TREE_CODE (init) == CONSTRUCTOR)
745 init = cp_fully_fold (init);
746 code = push_stmt_list ();
747 if (split_nonconstant_init_1 (dest, init))
748 init = NULL_TREE;
749 code = pop_stmt_list (code);
750 DECL_INITIAL (dest) = init;
751 TREE_READONLY (dest) = 0;
753 else if (TREE_CODE (init) == STRING_CST
754 && array_of_runtime_bound_p (TREE_TYPE (dest)))
755 code = build_vec_init (dest, NULL_TREE, init, /*value-init*/false,
756 /*from array*/1, tf_warning_or_error);
757 else
758 code = build2 (INIT_EXPR, TREE_TYPE (dest), dest, init);
760 return code;
763 /* Perform appropriate conversions on the initial value of a variable,
764 store it in the declaration DECL,
765 and print any error messages that are appropriate.
766 If the init is invalid, store an ERROR_MARK.
768 C++: Note that INIT might be a TREE_LIST, which would mean that it is
769 a base class initializer for some aggregate type, hopefully compatible
770 with DECL. If INIT is a single element, and DECL is an aggregate
771 type, we silently convert INIT into a TREE_LIST, allowing a constructor
772 to be called.
774 If INIT is a TREE_LIST and there is no constructor, turn INIT
775 into a CONSTRUCTOR and use standard initialization techniques.
776 Perhaps a warning should be generated?
778 Returns code to be executed if initialization could not be performed
779 for static variable. In that case, caller must emit the code. */
781 tree
782 store_init_value (tree decl, tree init, vec<tree, va_gc>** cleanups, int flags)
784 tree value, type;
786 /* If variable's type was invalidly declared, just ignore it. */
788 type = TREE_TYPE (decl);
789 if (TREE_CODE (type) == ERROR_MARK)
790 return NULL_TREE;
792 if (MAYBE_CLASS_TYPE_P (type))
794 if (TREE_CODE (init) == TREE_LIST)
796 error ("constructor syntax used, but no constructor declared "
797 "for type %qT", type);
798 init = build_constructor_from_list (init_list_type_node, nreverse (init));
802 /* End of special C++ code. */
804 if (flags & LOOKUP_ALREADY_DIGESTED)
805 value = init;
806 else
807 /* Digest the specified initializer into an expression. */
808 value = digest_init_flags (type, init, flags, tf_warning_or_error);
810 if (TREE_CODE (type) == ARRAY_TYPE
811 && TYPE_STRING_FLAG (TREE_TYPE (type))
812 && TREE_CODE (value) == CONSTRUCTOR)
813 value = braced_list_to_string (type, value);
815 value = extend_ref_init_temps (decl, value, cleanups);
817 /* In C++11 constant expression is a semantic, not syntactic, property.
818 In C++98, make sure that what we thought was a constant expression at
819 template definition time is still constant and otherwise perform this
820 as optimization, e.g. to fold SIZEOF_EXPRs in the initializer. */
821 if (decl_maybe_constant_var_p (decl) || TREE_STATIC (decl))
823 bool const_init;
824 tree oldval = value;
825 value = fold_non_dependent_expr (value);
826 if (DECL_DECLARED_CONSTEXPR_P (decl)
827 || (DECL_IN_AGGR_P (decl)
828 && DECL_INITIALIZED_IN_CLASS_P (decl)
829 && !DECL_VAR_DECLARED_INLINE_P (decl)))
831 /* Diagnose a non-constant initializer for constexpr variable or
832 non-inline in-class-initialized static data member. */
833 if (!require_constant_expression (value))
834 value = error_mark_node;
835 else
836 value = cxx_constant_init (value, decl);
838 else
839 value = maybe_constant_init (value, decl, true);
840 if (TREE_CODE (value) == CONSTRUCTOR && cp_has_mutable_p (type))
841 /* Poison this CONSTRUCTOR so it can't be copied to another
842 constexpr variable. */
843 CONSTRUCTOR_MUTABLE_POISON (value) = true;
844 const_init = (reduced_constant_expression_p (value)
845 || error_operand_p (value));
846 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = const_init;
847 /* FIXME setting TREE_CONSTANT on refs breaks the back end. */
848 if (!TYPE_REF_P (type))
849 TREE_CONSTANT (decl) = const_init && decl_maybe_constant_var_p (decl);
850 if (!const_init)
851 value = oldval;
853 value = cp_fully_fold (value);
855 /* Handle aggregate NSDMI in non-constant initializers, too. */
856 value = replace_placeholders (value, decl);
858 /* DECL may change value; purge caches. */
859 clear_cv_and_fold_caches ();
861 /* If the initializer is not a constant, fill in DECL_INITIAL with
862 the bits that are constant, and then return an expression that
863 will perform the dynamic initialization. */
864 if (value != error_mark_node
865 && (TREE_SIDE_EFFECTS (value)
866 || vla_type_p (type)
867 || ! reduced_constant_expression_p (value)))
868 return split_nonconstant_init (decl, value);
869 /* If the value is a constant, just put it in DECL_INITIAL. If DECL
870 is an automatic variable, the middle end will turn this into a
871 dynamic initialization later. */
872 DECL_INITIAL (decl) = value;
873 return NULL_TREE;
877 /* Give diagnostic about narrowing conversions within { }, or as part of
878 a converted constant expression. If CONST_ONLY, only check
879 constants. */
881 bool
882 check_narrowing (tree type, tree init, tsubst_flags_t complain, bool const_only)
884 tree ftype = unlowered_expr_type (init);
885 bool ok = true;
886 REAL_VALUE_TYPE d;
888 if (((!warn_narrowing || !(complain & tf_warning))
889 && cxx_dialect == cxx98)
890 || !ARITHMETIC_TYPE_P (type)
891 /* Don't emit bogus warnings with e.g. value-dependent trees. */
892 || instantiation_dependent_expression_p (init))
893 return ok;
895 if (BRACE_ENCLOSED_INITIALIZER_P (init)
896 && TREE_CODE (type) == COMPLEX_TYPE)
898 tree elttype = TREE_TYPE (type);
899 if (CONSTRUCTOR_NELTS (init) > 0)
900 ok &= check_narrowing (elttype, CONSTRUCTOR_ELT (init, 0)->value,
901 complain);
902 if (CONSTRUCTOR_NELTS (init) > 1)
903 ok &= check_narrowing (elttype, CONSTRUCTOR_ELT (init, 1)->value,
904 complain);
905 return ok;
908 init = maybe_constant_value (init);
910 /* If we were asked to only check constants, return early. */
911 if (const_only && !TREE_CONSTANT (init))
912 return ok;
914 if (CP_INTEGRAL_TYPE_P (type)
915 && TREE_CODE (ftype) == REAL_TYPE)
916 ok = false;
917 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype)
918 && CP_INTEGRAL_TYPE_P (type))
920 if (TREE_CODE (ftype) == ENUMERAL_TYPE)
921 /* Check for narrowing based on the values of the enumeration. */
922 ftype = ENUM_UNDERLYING_TYPE (ftype);
923 if ((tree_int_cst_lt (TYPE_MAX_VALUE (type),
924 TYPE_MAX_VALUE (ftype))
925 || tree_int_cst_lt (TYPE_MIN_VALUE (ftype),
926 TYPE_MIN_VALUE (type)))
927 && (TREE_CODE (init) != INTEGER_CST
928 || !int_fits_type_p (init, type)))
929 ok = false;
931 else if (TREE_CODE (ftype) == REAL_TYPE
932 && TREE_CODE (type) == REAL_TYPE)
934 if (TYPE_PRECISION (type) < TYPE_PRECISION (ftype))
936 if (TREE_CODE (init) == REAL_CST)
938 /* Issue 703: Loss of precision is OK as long as the value is
939 within the representable range of the new type. */
940 REAL_VALUE_TYPE r;
941 d = TREE_REAL_CST (init);
942 real_convert (&r, TYPE_MODE (type), &d);
943 if (real_isinf (&r))
944 ok = false;
946 else
947 ok = false;
950 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype)
951 && TREE_CODE (type) == REAL_TYPE)
953 ok = false;
954 if (TREE_CODE (init) == INTEGER_CST)
956 d = real_value_from_int_cst (0, init);
957 if (exact_real_truncate (TYPE_MODE (type), &d))
958 ok = true;
962 bool almost_ok = ok;
963 if (!ok && !CONSTANT_CLASS_P (init) && (complain & tf_warning_or_error))
965 tree folded = cp_fully_fold (init);
966 if (TREE_CONSTANT (folded) && check_narrowing (type, folded, tf_none))
967 almost_ok = true;
970 if (!ok)
972 location_t loc = cp_expr_loc_or_loc (init, input_location);
973 if (cxx_dialect == cxx98)
975 if (complain & tf_warning)
976 warning_at (loc, OPT_Wnarrowing, "narrowing conversion of %qE "
977 "from %qH to %qI is ill-formed in C++11",
978 init, ftype, type);
979 ok = true;
981 else if (!CONSTANT_CLASS_P (init))
983 if (complain & tf_warning_or_error)
985 auto_diagnostic_group d;
986 if ((!almost_ok || pedantic)
987 && pedwarn (loc, OPT_Wnarrowing,
988 "narrowing conversion of %qE from %qH to %qI",
989 init, ftype, type)
990 && almost_ok)
991 inform (loc, " the expression has a constant value but is not "
992 "a C++ constant-expression");
993 ok = true;
996 else if (complain & tf_error)
998 int savederrorcount = errorcount;
999 global_dc->pedantic_errors = 1;
1000 pedwarn (loc, OPT_Wnarrowing,
1001 "narrowing conversion of %qE from %qH to %qI ",
1002 init, ftype, type);
1003 if (errorcount == savederrorcount)
1004 ok = true;
1005 global_dc->pedantic_errors = flag_pedantic_errors;
1009 return ok;
1012 /* Process the initializer INIT for a variable of type TYPE, emitting
1013 diagnostics for invalid initializers and converting the initializer as
1014 appropriate.
1016 For aggregate types, it assumes that reshape_init has already run, thus the
1017 initializer will have the right shape (brace elision has been undone).
1019 NESTED is non-zero iff we are being called for an element of a CONSTRUCTOR,
1020 2 iff the element of a CONSTRUCTOR is inside another CONSTRUCTOR. */
1022 static tree
1023 digest_init_r (tree type, tree init, int nested, int flags,
1024 tsubst_flags_t complain)
1026 enum tree_code code = TREE_CODE (type);
1028 if (error_operand_p (init))
1029 return error_mark_node;
1031 gcc_assert (init);
1033 /* We must strip the outermost array type when completing the type,
1034 because the its bounds might be incomplete at the moment. */
1035 if (!complete_type_or_maybe_complain (code == ARRAY_TYPE
1036 ? TREE_TYPE (type) : type, NULL_TREE,
1037 complain))
1038 return error_mark_node;
1040 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue
1041 (g++.old-deja/g++.law/casts2.C). */
1042 if (TREE_CODE (init) == NON_LVALUE_EXPR)
1043 init = TREE_OPERAND (init, 0);
1045 location_t loc = cp_expr_loc_or_loc (init, input_location);
1047 /* Initialization of an array of chars from a string constant. The initializer
1048 can be optionally enclosed in braces, but reshape_init has already removed
1049 them if they were present. */
1050 if (code == ARRAY_TYPE)
1052 if (nested && !TYPE_DOMAIN (type))
1053 /* C++ flexible array members have a null domain. */
1054 pedwarn (loc, OPT_Wpedantic,
1055 "initialization of a flexible array member");
1057 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
1058 if (char_type_p (typ1)
1059 /*&& init */
1060 && TREE_CODE (init) == STRING_CST)
1062 tree char_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (init)));
1064 if (TYPE_PRECISION (typ1) == BITS_PER_UNIT)
1066 if (char_type != char_type_node)
1068 if (complain & tf_error)
1069 error_at (loc, "char-array initialized from wide string");
1070 return error_mark_node;
1073 else
1075 if (char_type == char_type_node)
1077 if (complain & tf_error)
1078 error_at (loc,
1079 "int-array initialized from non-wide string");
1080 return error_mark_node;
1082 else if (char_type != typ1)
1084 if (complain & tf_error)
1085 error_at (loc, "int-array initialized from incompatible "
1086 "wide string");
1087 return error_mark_node;
1091 if (nested == 2 && !TYPE_DOMAIN (type))
1093 if (complain & tf_error)
1094 error_at (loc, "initialization of flexible array member "
1095 "in a nested context");
1096 return error_mark_node;
1099 if (type != TREE_TYPE (init)
1100 && !variably_modified_type_p (type, NULL_TREE))
1102 init = copy_node (init);
1103 TREE_TYPE (init) = type;
1105 if (TYPE_DOMAIN (type) && TREE_CONSTANT (TYPE_SIZE (type)))
1107 /* Not a flexible array member. */
1108 int size = TREE_INT_CST_LOW (TYPE_SIZE (type));
1109 size = (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
1110 /* In C it is ok to subtract 1 from the length of the string
1111 because it's ok to ignore the terminating null char that is
1112 counted in the length of the constant, but in C++ this would
1113 be invalid. */
1114 if (size < TREE_STRING_LENGTH (init))
1116 permerror (loc, "initializer-string for array "
1117 "of chars is too long");
1119 init = build_string (size, TREE_STRING_POINTER (init));
1120 TREE_TYPE (init) = type;
1123 return init;
1127 /* Handle scalar types (including conversions) and references. */
1128 if ((code != COMPLEX_TYPE || BRACE_ENCLOSED_INITIALIZER_P (init))
1129 && (SCALAR_TYPE_P (type) || code == REFERENCE_TYPE))
1131 if (nested)
1132 flags |= LOOKUP_NO_NARROWING;
1133 init = convert_for_initialization (0, type, init, flags,
1134 ICR_INIT, NULL_TREE, 0,
1135 complain);
1137 return init;
1140 /* Come here only for aggregates: records, arrays, unions, complex numbers
1141 and vectors. */
1142 gcc_assert (code == ARRAY_TYPE
1143 || VECTOR_TYPE_P (type)
1144 || code == RECORD_TYPE
1145 || code == UNION_TYPE
1146 || code == COMPLEX_TYPE);
1148 /* "If T is a class type and the initializer list has a single
1149 element of type cv U, where U is T or a class derived from T,
1150 the object is initialized from that element." */
1151 if (flag_checking
1152 && cxx_dialect >= cxx11
1153 && BRACE_ENCLOSED_INITIALIZER_P (init)
1154 && CONSTRUCTOR_NELTS (init) == 1
1155 && ((CLASS_TYPE_P (type) && !CLASSTYPE_NON_AGGREGATE (type))
1156 || VECTOR_TYPE_P (type)))
1158 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
1159 if (reference_related_p (type, TREE_TYPE (elt)))
1160 /* We should have fixed this in reshape_init. */
1161 gcc_unreachable ();
1164 if (BRACE_ENCLOSED_INITIALIZER_P (init)
1165 && !TYPE_NON_AGGREGATE_CLASS (type))
1166 return process_init_constructor (type, init, nested, complain);
1167 else
1169 if (COMPOUND_LITERAL_P (init) && code == ARRAY_TYPE)
1171 if (complain & tf_error)
1172 error_at (loc, "cannot initialize aggregate of type %qT with "
1173 "a compound literal", type);
1175 return error_mark_node;
1178 if (code == ARRAY_TYPE
1179 && !BRACE_ENCLOSED_INITIALIZER_P (init))
1181 /* Allow the result of build_array_copy and of
1182 build_value_init_noctor. */
1183 if ((TREE_CODE (init) == VEC_INIT_EXPR
1184 || TREE_CODE (init) == CONSTRUCTOR)
1185 && (same_type_ignoring_top_level_qualifiers_p
1186 (type, TREE_TYPE (init))))
1187 return init;
1189 if (complain & tf_error)
1190 error_at (loc, "array must be initialized with a brace-enclosed"
1191 " initializer");
1192 return error_mark_node;
1195 return convert_for_initialization (NULL_TREE, type, init,
1196 flags,
1197 ICR_INIT, NULL_TREE, 0,
1198 complain);
1202 tree
1203 digest_init (tree type, tree init, tsubst_flags_t complain)
1205 return digest_init_r (type, init, 0, LOOKUP_IMPLICIT, complain);
1208 tree
1209 digest_init_flags (tree type, tree init, int flags, tsubst_flags_t complain)
1211 return digest_init_r (type, init, 0, flags, complain);
1214 /* Process the initializer INIT for an NSDMI DECL (a FIELD_DECL). */
1215 tree
1216 digest_nsdmi_init (tree decl, tree init, tsubst_flags_t complain)
1218 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
1220 tree type = TREE_TYPE (decl);
1221 int flags = LOOKUP_IMPLICIT;
1222 if (DIRECT_LIST_INIT_P (init))
1223 flags = LOOKUP_NORMAL;
1224 if (BRACE_ENCLOSED_INITIALIZER_P (init)
1225 && CP_AGGREGATE_TYPE_P (type))
1226 init = reshape_init (type, init, complain);
1227 init = digest_init_flags (type, init, flags, complain);
1228 if (TREE_CODE (init) == TARGET_EXPR)
1229 /* This represents the whole initialization. */
1230 TARGET_EXPR_DIRECT_INIT_P (init) = true;
1231 return init;
1234 /* Set of flags used within process_init_constructor to describe the
1235 initializers. */
1236 #define PICFLAG_ERRONEOUS 1
1237 #define PICFLAG_NOT_ALL_CONSTANT 2
1238 #define PICFLAG_NOT_ALL_SIMPLE 4
1239 #define PICFLAG_SIDE_EFFECTS 8
1241 /* Given an initializer INIT, return the flag (PICFLAG_*) which better
1242 describe it. */
1244 static int
1245 picflag_from_initializer (tree init)
1247 if (init == error_mark_node)
1248 return PICFLAG_ERRONEOUS;
1249 else if (!TREE_CONSTANT (init))
1251 if (TREE_SIDE_EFFECTS (init))
1252 return PICFLAG_SIDE_EFFECTS;
1253 else
1254 return PICFLAG_NOT_ALL_CONSTANT;
1256 else if (!initializer_constant_valid_p (init, TREE_TYPE (init)))
1257 return PICFLAG_NOT_ALL_SIMPLE;
1258 return 0;
1261 /* Adjust INIT for going into a CONSTRUCTOR. */
1263 static tree
1264 massage_init_elt (tree type, tree init, int nested, tsubst_flags_t complain)
1266 init = digest_init_r (type, init, nested ? 2 : 1, LOOKUP_IMPLICIT, complain);
1267 /* Strip a simple TARGET_EXPR when we know this is an initializer. */
1268 if (SIMPLE_TARGET_EXPR_P (init))
1269 init = TARGET_EXPR_INITIAL (init);
1270 /* When we defer constant folding within a statement, we may want to
1271 defer this folding as well. */
1272 tree t = fold_non_dependent_expr (init, complain);
1273 t = maybe_constant_init (t);
1274 if (TREE_CONSTANT (t))
1275 init = t;
1276 return init;
1279 /* Subroutine of process_init_constructor, which will process an initializer
1280 INIT for an array or vector of type TYPE. Returns the flags (PICFLAG_*)
1281 which describe the initializers. */
1283 static int
1284 process_init_constructor_array (tree type, tree init, int nested,
1285 tsubst_flags_t complain)
1287 unsigned HOST_WIDE_INT i, len = 0;
1288 int flags = 0;
1289 bool unbounded = false;
1290 constructor_elt *ce;
1291 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (init);
1293 gcc_assert (TREE_CODE (type) == ARRAY_TYPE
1294 || VECTOR_TYPE_P (type));
1296 if (TREE_CODE (type) == ARRAY_TYPE)
1298 /* C++ flexible array members have a null domain. */
1299 tree domain = TYPE_DOMAIN (type);
1300 if (domain && TREE_CONSTANT (TYPE_MAX_VALUE (domain)))
1301 len = wi::ext (wi::to_offset (TYPE_MAX_VALUE (domain))
1302 - wi::to_offset (TYPE_MIN_VALUE (domain)) + 1,
1303 TYPE_PRECISION (TREE_TYPE (domain)),
1304 TYPE_SIGN (TREE_TYPE (domain))).to_uhwi ();
1305 else
1306 unbounded = true; /* Take as many as there are. */
1308 if (nested == 2 && !domain && !vec_safe_is_empty (v))
1310 if (complain & tf_error)
1311 error_at (cp_expr_loc_or_loc (init, input_location),
1312 "initialization of flexible array member "
1313 "in a nested context");
1314 return PICFLAG_ERRONEOUS;
1317 else
1318 /* Vectors are like simple fixed-size arrays. */
1319 unbounded = !TYPE_VECTOR_SUBPARTS (type).is_constant (&len);
1321 /* There must not be more initializers than needed. */
1322 if (!unbounded && vec_safe_length (v) > len)
1324 if (complain & tf_error)
1325 error ("too many initializers for %qT", type);
1326 else
1327 return PICFLAG_ERRONEOUS;
1330 FOR_EACH_VEC_SAFE_ELT (v, i, ce)
1332 if (!ce->index)
1333 ce->index = size_int (i);
1334 else if (!check_array_designated_initializer (ce, i))
1335 ce->index = error_mark_node;
1336 gcc_assert (ce->value);
1337 ce->value
1338 = massage_init_elt (TREE_TYPE (type), ce->value, nested, complain);
1340 gcc_checking_assert
1341 (ce->value == error_mark_node
1342 || (same_type_ignoring_top_level_qualifiers_p
1343 (strip_array_types (TREE_TYPE (type)),
1344 strip_array_types (TREE_TYPE (ce->value)))));
1346 flags |= picflag_from_initializer (ce->value);
1349 /* No more initializers. If the array is unbounded, we are done. Otherwise,
1350 we must add initializers ourselves. */
1351 if (!unbounded)
1352 for (; i < len; ++i)
1354 tree next;
1356 if (type_build_ctor_call (TREE_TYPE (type)))
1358 /* If this type needs constructors run for default-initialization,
1359 we can't rely on the back end to do it for us, so make the
1360 initialization explicit by list-initializing from T{}. */
1361 next = build_constructor (init_list_type_node, NULL);
1362 next = massage_init_elt (TREE_TYPE (type), next, nested, complain);
1363 if (initializer_zerop (next))
1364 /* The default zero-initialization is fine for us; don't
1365 add anything to the CONSTRUCTOR. */
1366 next = NULL_TREE;
1368 else if (!zero_init_p (TREE_TYPE (type)))
1369 next = build_zero_init (TREE_TYPE (type),
1370 /*nelts=*/NULL_TREE,
1371 /*static_storage_p=*/false);
1372 else
1373 /* The default zero-initialization is fine for us; don't
1374 add anything to the CONSTRUCTOR. */
1375 next = NULL_TREE;
1377 if (next)
1379 flags |= picflag_from_initializer (next);
1380 if (len > i+1
1381 && (initializer_constant_valid_p (next, TREE_TYPE (next))
1382 == null_pointer_node))
1384 tree range = build2 (RANGE_EXPR, size_type_node,
1385 build_int_cst (size_type_node, i),
1386 build_int_cst (size_type_node, len - 1));
1387 CONSTRUCTOR_APPEND_ELT (v, range, next);
1388 break;
1390 else
1391 CONSTRUCTOR_APPEND_ELT (v, size_int (i), next);
1393 else
1394 /* Don't bother checking all the other elements. */
1395 break;
1398 CONSTRUCTOR_ELTS (init) = v;
1399 return flags;
1402 /* Subroutine of process_init_constructor, which will process an initializer
1403 INIT for a class of type TYPE. Returns the flags (PICFLAG_*) which describe
1404 the initializers. */
1406 static int
1407 process_init_constructor_record (tree type, tree init, int nested,
1408 tsubst_flags_t complain)
1410 vec<constructor_elt, va_gc> *v = NULL;
1411 tree field;
1412 int skipped = 0;
1414 gcc_assert (TREE_CODE (type) == RECORD_TYPE);
1415 gcc_assert (!CLASSTYPE_VBASECLASSES (type));
1416 gcc_assert (!TYPE_BINFO (type)
1417 || cxx_dialect >= cxx17
1418 || !BINFO_N_BASE_BINFOS (TYPE_BINFO (type)));
1419 gcc_assert (!TYPE_POLYMORPHIC_P (type));
1421 restart:
1422 int flags = 0;
1423 unsigned HOST_WIDE_INT idx = 0;
1424 int designator_skip = -1;
1425 /* Generally, we will always have an index for each initializer (which is
1426 a FIELD_DECL, put by reshape_init), but compound literals don't go trough
1427 reshape_init. So we need to handle both cases. */
1428 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
1430 tree next;
1431 tree type;
1433 if (TREE_CODE (field) != FIELD_DECL
1434 || (DECL_ARTIFICIAL (field)
1435 && !(cxx_dialect >= cxx17 && DECL_FIELD_IS_BASE (field))))
1436 continue;
1438 if (DECL_UNNAMED_BIT_FIELD (field))
1439 continue;
1441 /* If this is a bitfield, first convert to the declared type. */
1442 type = TREE_TYPE (field);
1443 if (DECL_BIT_FIELD_TYPE (field))
1444 type = DECL_BIT_FIELD_TYPE (field);
1445 if (type == error_mark_node)
1446 return PICFLAG_ERRONEOUS;
1448 next = NULL_TREE;
1449 if (idx < CONSTRUCTOR_NELTS (init))
1451 constructor_elt *ce = &(*CONSTRUCTOR_ELTS (init))[idx];
1452 if (ce->index)
1454 /* We can have either a FIELD_DECL or an IDENTIFIER_NODE. The
1455 latter case can happen in templates where lookup has to be
1456 deferred. */
1457 gcc_assert (TREE_CODE (ce->index) == FIELD_DECL
1458 || identifier_p (ce->index));
1459 if (ce->index == field || ce->index == DECL_NAME (field))
1460 next = ce->value;
1461 else if (ANON_AGGR_TYPE_P (type)
1462 && search_anon_aggr (type,
1463 TREE_CODE (ce->index) == FIELD_DECL
1464 ? DECL_NAME (ce->index)
1465 : ce->index))
1466 /* If the element is an anonymous union object and the
1467 initializer list is a designated-initializer-list, the
1468 anonymous union object is initialized by the
1469 designated-initializer-list { D }, where D is the
1470 designated-initializer-clause naming a member of the
1471 anonymous union object. */
1472 next = build_constructor_single (init_list_type_node,
1473 ce->index, ce->value);
1474 else
1476 ce = NULL;
1477 if (designator_skip == -1)
1478 designator_skip = 1;
1481 else
1483 designator_skip = 0;
1484 next = ce->value;
1487 if (ce)
1489 gcc_assert (ce->value);
1490 next = massage_init_elt (type, next, nested, complain);
1491 ++idx;
1494 if (next)
1495 /* Already handled above. */;
1496 else if (DECL_INITIAL (field))
1498 if (skipped > 0)
1500 /* We're using an NSDMI past a field with implicit
1501 zero-init. Go back and make it explicit. */
1502 skipped = -1;
1503 vec_safe_truncate (v, 0);
1504 goto restart;
1506 /* C++14 aggregate NSDMI. */
1507 next = get_nsdmi (field, /*ctor*/false, complain);
1508 if (!CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init)
1509 && find_placeholders (next))
1510 CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init) = 1;
1512 else if (type_build_ctor_call (TREE_TYPE (field)))
1514 /* If this type needs constructors run for
1515 default-initialization, we can't rely on the back end to do it
1516 for us, so build up TARGET_EXPRs. If the type in question is
1517 a class, just build one up; if it's an array, recurse. */
1518 next = build_constructor (init_list_type_node, NULL);
1519 next = massage_init_elt (TREE_TYPE (field), next, nested, complain);
1521 /* Warn when some struct elements are implicitly initialized. */
1522 if ((complain & tf_warning)
1523 && !EMPTY_CONSTRUCTOR_P (init))
1524 warning (OPT_Wmissing_field_initializers,
1525 "missing initializer for member %qD", field);
1527 else
1529 const_tree fldtype = TREE_TYPE (field);
1530 if (TYPE_REF_P (fldtype))
1532 if (complain & tf_error)
1533 error ("member %qD is uninitialized reference", field);
1534 else
1535 return PICFLAG_ERRONEOUS;
1537 else if (CLASSTYPE_REF_FIELDS_NEED_INIT (fldtype))
1539 if (complain & tf_error)
1540 error ("member %qD with uninitialized reference fields", field);
1541 else
1542 return PICFLAG_ERRONEOUS;
1545 /* Warn when some struct elements are implicitly initialized
1546 to zero. However, avoid issuing the warning for flexible
1547 array members since they need not have any elements. */
1548 if ((TREE_CODE (fldtype) != ARRAY_TYPE || TYPE_DOMAIN (fldtype))
1549 && (complain & tf_warning)
1550 && !EMPTY_CONSTRUCTOR_P (init))
1551 warning (OPT_Wmissing_field_initializers,
1552 "missing initializer for member %qD", field);
1554 if (!zero_init_p (fldtype)
1555 || skipped < 0)
1556 next = build_zero_init (TREE_TYPE (field), /*nelts=*/NULL_TREE,
1557 /*static_storage_p=*/false);
1558 else
1560 /* The default zero-initialization is fine for us; don't
1561 add anything to the CONSTRUCTOR. */
1562 skipped = 1;
1563 continue;
1567 /* If this is a bitfield, now convert to the lowered type. */
1568 if (type != TREE_TYPE (field))
1569 next = cp_convert_and_check (TREE_TYPE (field), next, complain);
1570 flags |= picflag_from_initializer (next);
1571 CONSTRUCTOR_APPEND_ELT (v, field, next);
1574 if (idx < CONSTRUCTOR_NELTS (init))
1576 if (complain & tf_error)
1578 constructor_elt *ce = &(*CONSTRUCTOR_ELTS (init))[idx];
1579 /* For better diagnostics, try to find out if it is really
1580 the case of too many initializers or if designators are
1581 in incorrect order. */
1582 if (designator_skip == 1 && ce->index)
1584 gcc_assert (TREE_CODE (ce->index) == FIELD_DECL
1585 || identifier_p (ce->index));
1586 for (field = TYPE_FIELDS (type);
1587 field; field = DECL_CHAIN (field))
1589 if (TREE_CODE (field) != FIELD_DECL
1590 || (DECL_ARTIFICIAL (field)
1591 && !(cxx_dialect >= cxx17
1592 && DECL_FIELD_IS_BASE (field))))
1593 continue;
1595 if (DECL_UNNAMED_BIT_FIELD (field))
1596 continue;
1598 if (ce->index == field || ce->index == DECL_NAME (field))
1599 break;
1600 if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1602 tree t
1603 = search_anon_aggr (TREE_TYPE (field),
1604 TREE_CODE (ce->index) == FIELD_DECL
1605 ? DECL_NAME (ce->index)
1606 : ce->index);
1607 if (t)
1609 field = t;
1610 break;
1615 if (field)
1616 error ("designator order for field %qD does not match declaration "
1617 "order in %qT", field, type);
1618 else
1619 error ("too many initializers for %qT", type);
1621 else
1622 return PICFLAG_ERRONEOUS;
1625 CONSTRUCTOR_ELTS (init) = v;
1626 return flags;
1629 /* Subroutine of process_init_constructor, which will process a single
1630 initializer INIT for a union of type TYPE. Returns the flags (PICFLAG_*)
1631 which describe the initializer. */
1633 static int
1634 process_init_constructor_union (tree type, tree init, int nested,
1635 tsubst_flags_t complain)
1637 constructor_elt *ce;
1638 int len;
1640 /* If the initializer was empty, use the union's NSDMI if it has one.
1641 Otherwise use default zero initialization. */
1642 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
1644 for (tree field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1646 if (TREE_CODE (field) == FIELD_DECL
1647 && DECL_INITIAL (field) != NULL_TREE)
1649 tree val = get_nsdmi (field, /*in_ctor=*/false, complain);
1650 if (!CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init)
1651 && find_placeholders (val))
1652 CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init) = 1;
1653 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (init), field, val);
1654 break;
1658 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
1659 return 0;
1662 len = CONSTRUCTOR_ELTS (init)->length ();
1663 if (len > 1)
1665 if (!(complain & tf_error))
1666 return PICFLAG_ERRONEOUS;
1667 error ("too many initializers for %qT", type);
1668 CONSTRUCTOR_ELTS (init)->block_remove (1, len-1);
1671 ce = &(*CONSTRUCTOR_ELTS (init))[0];
1673 /* If this element specifies a field, initialize via that field. */
1674 if (ce->index)
1676 if (TREE_CODE (ce->index) == FIELD_DECL)
1678 else if (identifier_p (ce->index))
1680 /* This can happen within a cast, see g++.dg/opt/cse2.C. */
1681 tree name = ce->index;
1682 tree field;
1683 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1684 if (DECL_NAME (field) == name)
1685 break;
1686 if (!field)
1688 if (complain & tf_error)
1689 error ("no field %qD found in union being initialized",
1690 field);
1691 ce->value = error_mark_node;
1693 ce->index = field;
1695 else
1697 gcc_assert (TREE_CODE (ce->index) == INTEGER_CST
1698 || TREE_CODE (ce->index) == RANGE_EXPR);
1699 if (complain & tf_error)
1700 error ("index value instead of field name in union initializer");
1701 ce->value = error_mark_node;
1704 else
1706 /* Find the first named field. ANSI decided in September 1990
1707 that only named fields count here. */
1708 tree field = TYPE_FIELDS (type);
1709 while (field && (!DECL_NAME (field) || TREE_CODE (field) != FIELD_DECL))
1710 field = TREE_CHAIN (field);
1711 if (field == NULL_TREE)
1713 if (complain & tf_error)
1714 error ("too many initializers for %qT", type);
1715 ce->value = error_mark_node;
1717 ce->index = field;
1720 if (ce->value && ce->value != error_mark_node)
1721 ce->value = massage_init_elt (TREE_TYPE (ce->index), ce->value, nested,
1722 complain);
1724 return picflag_from_initializer (ce->value);
1727 /* Process INIT, a constructor for a variable of aggregate type TYPE. The
1728 constructor is a brace-enclosed initializer, and will be modified in-place.
1730 Each element is converted to the right type through digest_init, and
1731 missing initializers are added following the language rules (zero-padding,
1732 etc.).
1734 After the execution, the initializer will have TREE_CONSTANT if all elts are
1735 constant, and TREE_STATIC set if, in addition, all elts are simple enough
1736 constants that the assembler and linker can compute them.
1738 The function returns the initializer itself, or error_mark_node in case
1739 of error. */
1741 static tree
1742 process_init_constructor (tree type, tree init, int nested,
1743 tsubst_flags_t complain)
1745 int flags;
1747 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
1749 if (TREE_CODE (type) == ARRAY_TYPE || VECTOR_TYPE_P (type))
1750 flags = process_init_constructor_array (type, init, nested, complain);
1751 else if (TREE_CODE (type) == RECORD_TYPE)
1752 flags = process_init_constructor_record (type, init, nested, complain);
1753 else if (TREE_CODE (type) == UNION_TYPE)
1754 flags = process_init_constructor_union (type, init, nested, complain);
1755 else
1756 gcc_unreachable ();
1758 if (flags & PICFLAG_ERRONEOUS)
1759 return error_mark_node;
1761 TREE_TYPE (init) = type;
1762 if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == NULL_TREE)
1763 cp_complete_array_type (&TREE_TYPE (init), init, /*do_default=*/0);
1764 if (flags & PICFLAG_SIDE_EFFECTS)
1766 TREE_CONSTANT (init) = false;
1767 TREE_SIDE_EFFECTS (init) = true;
1769 else if (flags & PICFLAG_NOT_ALL_CONSTANT)
1770 /* Make sure TREE_CONSTANT isn't set from build_constructor. */
1771 TREE_CONSTANT (init) = false;
1772 else
1774 TREE_CONSTANT (init) = 1;
1775 if (!(flags & PICFLAG_NOT_ALL_SIMPLE))
1776 TREE_STATIC (init) = 1;
1778 return init;
1781 /* Given a structure or union value DATUM, construct and return
1782 the structure or union component which results from narrowing
1783 that value to the base specified in BASETYPE. For example, given the
1784 hierarchy
1786 class L { int ii; };
1787 class A : L { ... };
1788 class B : L { ... };
1789 class C : A, B { ... };
1791 and the declaration
1793 C x;
1795 then the expression
1797 x.A::ii refers to the ii member of the L part of
1798 the A part of the C object named by X. In this case,
1799 DATUM would be x, and BASETYPE would be A.
1801 I used to think that this was nonconformant, that the standard specified
1802 that first we look up ii in A, then convert x to an L& and pull out the
1803 ii part. But in fact, it does say that we convert x to an A&; A here
1804 is known as the "naming class". (jason 2000-12-19)
1806 BINFO_P points to a variable initialized either to NULL_TREE or to the
1807 binfo for the specific base subobject we want to convert to. */
1809 tree
1810 build_scoped_ref (tree datum, tree basetype, tree* binfo_p)
1812 tree binfo;
1814 if (datum == error_mark_node)
1815 return error_mark_node;
1816 if (*binfo_p)
1817 binfo = *binfo_p;
1818 else
1819 binfo = lookup_base (TREE_TYPE (datum), basetype, ba_check,
1820 NULL, tf_warning_or_error);
1822 if (!binfo || binfo == error_mark_node)
1824 *binfo_p = NULL_TREE;
1825 if (!binfo)
1826 error_not_base_type (basetype, TREE_TYPE (datum));
1827 return error_mark_node;
1830 *binfo_p = binfo;
1831 return build_base_path (PLUS_EXPR, datum, binfo, 1,
1832 tf_warning_or_error);
1835 /* Build a reference to an object specified by the C++ `->' operator.
1836 Usually this just involves dereferencing the object, but if the
1837 `->' operator is overloaded, then such overloads must be
1838 performed until an object which does not have the `->' operator
1839 overloaded is found. An error is reported when circular pointer
1840 delegation is detected. */
1842 tree
1843 build_x_arrow (location_t loc, tree expr, tsubst_flags_t complain)
1845 tree orig_expr = expr;
1846 tree type = TREE_TYPE (expr);
1847 tree last_rval = NULL_TREE;
1848 vec<tree, va_gc> *types_memoized = NULL;
1850 if (type == error_mark_node)
1851 return error_mark_node;
1853 if (processing_template_decl)
1855 if (type && TYPE_PTR_P (type)
1856 && !dependent_scope_p (TREE_TYPE (type)))
1857 /* Pointer to current instantiation, don't treat as dependent. */;
1858 else if (type_dependent_expression_p (expr))
1859 return build_min_nt_loc (loc, ARROW_EXPR, expr);
1860 expr = build_non_dependent_expr (expr);
1863 if (MAYBE_CLASS_TYPE_P (type))
1865 struct tinst_level *actual_inst = current_instantiation ();
1866 tree fn = NULL;
1868 while ((expr = build_new_op (loc, COMPONENT_REF,
1869 LOOKUP_NORMAL, expr, NULL_TREE, NULL_TREE,
1870 &fn, complain)))
1872 if (expr == error_mark_node)
1873 return error_mark_node;
1875 /* This provides a better instantiation backtrace in case of
1876 error. */
1877 if (fn && DECL_USE_TEMPLATE (fn))
1878 push_tinst_level_loc (fn,
1879 (current_instantiation () != actual_inst)
1880 ? DECL_SOURCE_LOCATION (fn)
1881 : input_location);
1882 fn = NULL;
1884 if (vec_member (TREE_TYPE (expr), types_memoized))
1886 if (complain & tf_error)
1887 error ("circular pointer delegation detected");
1888 return error_mark_node;
1891 vec_safe_push (types_memoized, TREE_TYPE (expr));
1892 last_rval = expr;
1895 while (current_instantiation () != actual_inst)
1896 pop_tinst_level ();
1898 if (last_rval == NULL_TREE)
1900 if (complain & tf_error)
1901 error ("base operand of %<->%> has non-pointer type %qT", type);
1902 return error_mark_node;
1905 if (TYPE_REF_P (TREE_TYPE (last_rval)))
1906 last_rval = convert_from_reference (last_rval);
1908 else
1909 last_rval = decay_conversion (expr, complain);
1911 if (TYPE_PTR_P (TREE_TYPE (last_rval)))
1913 if (processing_template_decl)
1915 expr = build_min (ARROW_EXPR, TREE_TYPE (TREE_TYPE (last_rval)),
1916 orig_expr);
1917 TREE_SIDE_EFFECTS (expr) = TREE_SIDE_EFFECTS (last_rval);
1918 return expr;
1921 return cp_build_indirect_ref (last_rval, RO_ARROW, complain);
1924 if (complain & tf_error)
1926 if (types_memoized)
1927 error ("result of %<operator->()%> yields non-pointer result");
1928 else
1929 error ("base operand of %<->%> is not a pointer");
1931 return error_mark_node;
1934 /* Return an expression for "DATUM .* COMPONENT". DATUM has not
1935 already been checked out to be of aggregate type. */
1937 tree
1938 build_m_component_ref (tree datum, tree component, tsubst_flags_t complain)
1940 tree ptrmem_type;
1941 tree objtype;
1942 tree type;
1943 tree binfo;
1944 tree ctype;
1946 if (error_operand_p (datum) || error_operand_p (component))
1947 return error_mark_node;
1949 datum = mark_lvalue_use (datum);
1950 component = mark_rvalue_use (component);
1952 ptrmem_type = TREE_TYPE (component);
1953 if (!TYPE_PTRMEM_P (ptrmem_type))
1955 if (complain & tf_error)
1956 error ("%qE cannot be used as a member pointer, since it is of "
1957 "type %qT", component, ptrmem_type);
1958 return error_mark_node;
1961 objtype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
1962 if (! MAYBE_CLASS_TYPE_P (objtype))
1964 if (complain & tf_error)
1965 error ("cannot apply member pointer %qE to %qE, which is of "
1966 "non-class type %qT", component, datum, objtype);
1967 return error_mark_node;
1970 type = TYPE_PTRMEM_POINTED_TO_TYPE (ptrmem_type);
1971 ctype = complete_type (TYPE_PTRMEM_CLASS_TYPE (ptrmem_type));
1973 if (!COMPLETE_TYPE_P (ctype))
1975 if (!same_type_p (ctype, objtype))
1976 goto mismatch;
1977 binfo = NULL;
1979 else
1981 binfo = lookup_base (objtype, ctype, ba_check, NULL, complain);
1983 if (!binfo)
1985 mismatch:
1986 if (complain & tf_error)
1987 error ("pointer to member type %qT incompatible with object "
1988 "type %qT", type, objtype);
1989 return error_mark_node;
1991 else if (binfo == error_mark_node)
1992 return error_mark_node;
1995 if (TYPE_PTRDATAMEM_P (ptrmem_type))
1997 bool is_lval = real_lvalue_p (datum);
1998 tree ptype;
2000 /* Compute the type of the field, as described in [expr.ref].
2001 There's no such thing as a mutable pointer-to-member, so
2002 things are not as complex as they are for references to
2003 non-static data members. */
2004 type = cp_build_qualified_type (type,
2005 (cp_type_quals (type)
2006 | cp_type_quals (TREE_TYPE (datum))));
2008 datum = build_address (datum);
2010 /* Convert object to the correct base. */
2011 if (binfo)
2013 datum = build_base_path (PLUS_EXPR, datum, binfo, 1, complain);
2014 if (datum == error_mark_node)
2015 return error_mark_node;
2018 /* Build an expression for "object + offset" where offset is the
2019 value stored in the pointer-to-data-member. */
2020 ptype = build_pointer_type (type);
2021 datum = fold_build_pointer_plus (fold_convert (ptype, datum), component);
2022 datum = cp_build_fold_indirect_ref (datum);
2023 if (datum == error_mark_node)
2024 return error_mark_node;
2026 /* If the object expression was an rvalue, return an rvalue. */
2027 if (!is_lval)
2028 datum = move (datum);
2029 return datum;
2031 else
2033 /* 5.5/6: In a .* expression whose object expression is an rvalue, the
2034 program is ill-formed if the second operand is a pointer to member
2035 function with ref-qualifier & (for C++2A: unless its cv-qualifier-seq
2036 is const). In a .* expression whose object expression is an lvalue,
2037 the program is ill-formed if the second operand is a pointer to member
2038 function with ref-qualifier &&. */
2039 if (FUNCTION_REF_QUALIFIED (type))
2041 bool lval = lvalue_p (datum);
2042 if (lval && FUNCTION_RVALUE_QUALIFIED (type))
2044 if (complain & tf_error)
2045 error ("pointer-to-member-function type %qT requires an rvalue",
2046 ptrmem_type);
2047 return error_mark_node;
2049 else if (!lval && !FUNCTION_RVALUE_QUALIFIED (type))
2051 if ((type_memfn_quals (type)
2052 & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE))
2053 != TYPE_QUAL_CONST)
2055 if (complain & tf_error)
2056 error ("pointer-to-member-function type %qT requires "
2057 "an lvalue", ptrmem_type);
2058 return error_mark_node;
2060 else if (cxx_dialect < cxx2a)
2062 if (complain & tf_warning_or_error)
2063 pedwarn (input_location, OPT_Wpedantic,
2064 "pointer-to-member-function type %qT requires "
2065 "an lvalue before C++2a", ptrmem_type);
2066 else
2067 return error_mark_node;
2071 return build2 (OFFSET_REF, type, datum, component);
2075 /* Return a tree node for the expression TYPENAME '(' PARMS ')'. */
2077 tree
2078 build_functional_cast (tree exp, tree parms, tsubst_flags_t complain)
2080 /* This is either a call to a constructor,
2081 or a C cast in C++'s `functional' notation. */
2083 /* The type to which we are casting. */
2084 tree type;
2085 vec<tree, va_gc> *parmvec;
2087 if (error_operand_p (exp) || parms == error_mark_node)
2088 return error_mark_node;
2090 if (TREE_CODE (exp) == TYPE_DECL)
2092 type = TREE_TYPE (exp);
2094 if (DECL_ARTIFICIAL (exp))
2095 cp_warn_deprecated_use (type);
2097 else
2098 type = exp;
2100 /* We need to check this explicitly, since value-initialization of
2101 arrays is allowed in other situations. */
2102 if (TREE_CODE (type) == ARRAY_TYPE)
2104 if (complain & tf_error)
2105 error ("functional cast to array type %qT", type);
2106 return error_mark_node;
2109 if (tree anode = type_uses_auto (type))
2111 if (!CLASS_PLACEHOLDER_TEMPLATE (anode))
2113 if (complain & tf_error)
2114 error_at (DECL_SOURCE_LOCATION (TEMPLATE_TYPE_DECL (anode)),
2115 "invalid use of %qT", anode);
2116 return error_mark_node;
2118 else if (!parms)
2120 if (complain & tf_error)
2121 error ("cannot deduce template arguments for %qT from ()", anode);
2122 return error_mark_node;
2124 else
2125 type = do_auto_deduction (type, parms, anode, complain,
2126 adc_variable_type);
2129 if (processing_template_decl)
2131 tree t;
2133 /* Diagnose this even in a template. We could also try harder
2134 to give all the usual errors when the type and args are
2135 non-dependent... */
2136 if (TYPE_REF_P (type) && !parms)
2138 if (complain & tf_error)
2139 error ("invalid value-initialization of reference type");
2140 return error_mark_node;
2143 t = build_min (CAST_EXPR, type, parms);
2144 /* We don't know if it will or will not have side effects. */
2145 TREE_SIDE_EFFECTS (t) = 1;
2146 return t;
2149 if (! MAYBE_CLASS_TYPE_P (type))
2151 if (parms == NULL_TREE)
2153 if (VOID_TYPE_P (type))
2154 return void_node;
2155 return build_value_init (cv_unqualified (type), complain);
2158 /* This must build a C cast. */
2159 parms = build_x_compound_expr_from_list (parms, ELK_FUNC_CAST, complain);
2160 return cp_build_c_cast (type, parms, complain);
2163 /* Prepare to evaluate as a call to a constructor. If this expression
2164 is actually used, for example,
2166 return X (arg1, arg2, ...);
2168 then the slot being initialized will be filled in. */
2170 if (!complete_type_or_maybe_complain (type, NULL_TREE, complain))
2171 return error_mark_node;
2172 if (abstract_virtuals_error_sfinae (ACU_CAST, type, complain))
2173 return error_mark_node;
2175 /* [expr.type.conv]
2177 If the expression list is a single-expression, the type
2178 conversion is equivalent (in definedness, and if defined in
2179 meaning) to the corresponding cast expression. */
2180 if (parms && TREE_CHAIN (parms) == NULL_TREE)
2181 return cp_build_c_cast (type, TREE_VALUE (parms), complain);
2183 /* [expr.type.conv]
2185 The expression T(), where T is a simple-type-specifier for a
2186 non-array complete object type or the (possibly cv-qualified)
2187 void type, creates an rvalue of the specified type, which is
2188 value-initialized. */
2190 if (parms == NULL_TREE)
2192 exp = build_value_init (type, complain);
2193 exp = get_target_expr_sfinae (exp, complain);
2194 return exp;
2197 /* Call the constructor. */
2198 parmvec = make_tree_vector ();
2199 for (; parms != NULL_TREE; parms = TREE_CHAIN (parms))
2200 vec_safe_push (parmvec, TREE_VALUE (parms));
2201 exp = build_special_member_call (NULL_TREE, complete_ctor_identifier,
2202 &parmvec, type, LOOKUP_NORMAL, complain);
2203 release_tree_vector (parmvec);
2205 if (exp == error_mark_node)
2206 return error_mark_node;
2208 return build_cplus_new (type, exp, complain);
2212 /* Add new exception specifier SPEC, to the LIST we currently have.
2213 If it's already in LIST then do nothing.
2214 Moan if it's bad and we're allowed to. COMPLAIN < 0 means we
2215 know what we're doing. */
2217 tree
2218 add_exception_specifier (tree list, tree spec, tsubst_flags_t complain)
2220 bool ok;
2221 tree core = spec;
2222 bool is_ptr;
2223 diagnostic_t diag_type = DK_UNSPECIFIED; /* none */
2225 if (spec == error_mark_node)
2226 return list;
2228 gcc_assert (spec && (!list || TREE_VALUE (list)));
2230 /* [except.spec] 1, type in an exception specifier shall not be
2231 incomplete, or pointer or ref to incomplete other than pointer
2232 to cv void. */
2233 is_ptr = TYPE_PTR_P (core);
2234 if (is_ptr || TYPE_REF_P (core))
2235 core = TREE_TYPE (core);
2236 if (complain < 0)
2237 ok = true;
2238 else if (VOID_TYPE_P (core))
2239 ok = is_ptr;
2240 else if (TREE_CODE (core) == TEMPLATE_TYPE_PARM)
2241 ok = true;
2242 else if (processing_template_decl)
2243 ok = true;
2244 else
2246 ok = true;
2247 /* 15.4/1 says that types in an exception specifier must be complete,
2248 but it seems more reasonable to only require this on definitions
2249 and calls. So just give a pedwarn at this point; we will give an
2250 error later if we hit one of those two cases. */
2251 if (!COMPLETE_TYPE_P (complete_type (core)))
2252 diag_type = DK_PEDWARN; /* pedwarn */
2255 if (ok)
2257 tree probe;
2259 for (probe = list; probe; probe = TREE_CHAIN (probe))
2260 if (same_type_p (TREE_VALUE (probe), spec))
2261 break;
2262 if (!probe)
2263 list = tree_cons (NULL_TREE, spec, list);
2265 else
2266 diag_type = DK_ERROR; /* error */
2268 if (diag_type != DK_UNSPECIFIED
2269 && (complain & tf_warning_or_error))
2270 cxx_incomplete_type_diagnostic (NULL_TREE, core, diag_type);
2272 return list;
2275 /* Like nothrow_spec_p, but don't abort on deferred noexcept. */
2277 static bool
2278 nothrow_spec_p_uninst (const_tree spec)
2280 if (DEFERRED_NOEXCEPT_SPEC_P (spec))
2281 return false;
2282 return nothrow_spec_p (spec);
2285 /* Combine the two exceptions specifier lists LIST and ADD, and return
2286 their union. */
2288 tree
2289 merge_exception_specifiers (tree list, tree add)
2291 tree noex, orig_list;
2293 /* No exception-specifier or noexcept(false) are less strict than
2294 anything else. Prefer the newer variant (LIST). */
2295 if (!list || list == noexcept_false_spec)
2296 return list;
2297 else if (!add || add == noexcept_false_spec)
2298 return add;
2300 /* noexcept(true) and throw() are stricter than anything else.
2301 As above, prefer the more recent one (LIST). */
2302 if (nothrow_spec_p_uninst (add))
2303 return list;
2305 /* Two implicit noexcept specs (e.g. on a destructor) are equivalent. */
2306 if (UNEVALUATED_NOEXCEPT_SPEC_P (add)
2307 && UNEVALUATED_NOEXCEPT_SPEC_P (list))
2308 return list;
2309 /* We should have instantiated other deferred noexcept specs by now. */
2310 gcc_assert (!DEFERRED_NOEXCEPT_SPEC_P (add));
2312 if (nothrow_spec_p_uninst (list))
2313 return add;
2314 noex = TREE_PURPOSE (list);
2315 gcc_checking_assert (!TREE_PURPOSE (add)
2316 || errorcount || !flag_exceptions
2317 || cp_tree_equal (noex, TREE_PURPOSE (add)));
2319 /* Combine the dynamic-exception-specifiers, if any. */
2320 orig_list = list;
2321 for (; add && TREE_VALUE (add); add = TREE_CHAIN (add))
2323 tree spec = TREE_VALUE (add);
2324 tree probe;
2326 for (probe = orig_list; probe && TREE_VALUE (probe);
2327 probe = TREE_CHAIN (probe))
2328 if (same_type_p (TREE_VALUE (probe), spec))
2329 break;
2330 if (!probe)
2332 spec = build_tree_list (NULL_TREE, spec);
2333 TREE_CHAIN (spec) = list;
2334 list = spec;
2338 /* Keep the noexcept-specifier at the beginning of the list. */
2339 if (noex != TREE_PURPOSE (list))
2340 list = tree_cons (noex, TREE_VALUE (list), TREE_CHAIN (list));
2342 return list;
2345 /* Subroutine of build_call. Ensure that each of the types in the
2346 exception specification is complete. Technically, 15.4/1 says that
2347 they need to be complete when we see a declaration of the function,
2348 but we should be able to get away with only requiring this when the
2349 function is defined or called. See also add_exception_specifier. */
2351 void
2352 require_complete_eh_spec_types (tree fntype, tree decl)
2354 tree raises;
2355 /* Don't complain about calls to op new. */
2356 if (decl && DECL_ARTIFICIAL (decl))
2357 return;
2358 for (raises = TYPE_RAISES_EXCEPTIONS (fntype); raises;
2359 raises = TREE_CHAIN (raises))
2361 tree type = TREE_VALUE (raises);
2362 if (type && !COMPLETE_TYPE_P (type))
2364 if (decl)
2365 error
2366 ("call to function %qD which throws incomplete type %q#T",
2367 decl, type);
2368 else
2369 error ("call to function which throws incomplete type %q#T",
2370 decl);
2376 #include "gt-cp-typeck2.h"