re PR c++/63757 (nullptr conversion sequence fails to compile)
[official-gcc.git] / gcc / cp / typeck2.c
blob92c04178c213f694170462b958e4453afb0ddb91
1 /* Report error messages, build initializers, and perform
2 some front-end optimizations for C++ compiler.
3 Copyright (C) 1987-2014 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 "tm.h"
32 #include "tree.h"
33 #include "stor-layout.h"
34 #include "varasm.h"
35 #include "intl.h"
36 #include "cp-tree.h"
37 #include "flags.h"
38 #include "diagnostic-core.h"
39 #include "wide-int.h"
41 static tree
42 process_init_constructor (tree type, tree init, tsubst_flags_t complain);
45 /* Print an error message stemming from an attempt to use
46 BASETYPE as a base class for TYPE. */
48 tree
49 error_not_base_type (tree basetype, tree type)
51 if (TREE_CODE (basetype) == FUNCTION_DECL)
52 basetype = DECL_CONTEXT (basetype);
53 error ("type %qT is not a base type for type %qT", basetype, type);
54 return error_mark_node;
57 tree
58 binfo_or_else (tree base, tree type)
60 tree binfo = lookup_base (type, base, ba_unique,
61 NULL, tf_warning_or_error);
63 if (binfo == error_mark_node)
64 return NULL_TREE;
65 else if (!binfo)
66 error_not_base_type (base, type);
67 return binfo;
70 /* According to ARM $7.1.6, "A `const' object may be initialized, but its
71 value may not be changed thereafter. */
73 void
74 cxx_readonly_error (tree arg, enum lvalue_use errstring)
77 /* This macro is used to emit diagnostics to ensure that all format
78 strings are complete sentences, visible to gettext and checked at
79 compile time. */
81 #define ERROR_FOR_ASSIGNMENT(AS, ASM, IN, DE, ARG) \
82 do { \
83 switch (errstring) \
84 { \
85 case lv_assign: \
86 error(AS, ARG); \
87 break; \
88 case lv_asm: \
89 error(ASM, ARG); \
90 break; \
91 case lv_increment: \
92 error (IN, ARG); \
93 break; \
94 case lv_decrement: \
95 error (DE, ARG); \
96 break; \
97 default: \
98 gcc_unreachable (); \
99 } \
100 } while (0)
102 /* Handle C++-specific things first. */
104 if (VAR_P (arg)
105 && DECL_LANG_SPECIFIC (arg)
106 && DECL_IN_AGGR_P (arg)
107 && !TREE_STATIC (arg))
108 ERROR_FOR_ASSIGNMENT (G_("assignment of "
109 "constant field %qD"),
110 G_("constant field %qD "
111 "used as %<asm%> output"),
112 G_("increment of "
113 "constant field %qD"),
114 G_("decrement of "
115 "constant field %qD"),
116 arg);
117 else if (INDIRECT_REF_P (arg)
118 && TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 0))) == REFERENCE_TYPE
119 && (VAR_P (TREE_OPERAND (arg, 0))
120 || TREE_CODE (TREE_OPERAND (arg, 0)) == PARM_DECL))
121 ERROR_FOR_ASSIGNMENT (G_("assignment of "
122 "read-only reference %qD"),
123 G_("read-only reference %qD "
124 "used as %<asm%> output"),
125 G_("increment of "
126 "read-only reference %qD"),
127 G_("decrement of "
128 "read-only reference %qD"),
129 TREE_OPERAND (arg, 0));
130 else
131 readonly_error (input_location, arg, errstring);
135 /* Structure that holds information about declarations whose type was
136 incomplete and we could not check whether it was abstract or not. */
138 struct GTY((chain_next ("%h.next"), for_user)) pending_abstract_type {
139 /* Declaration which we are checking for abstractness. It is either
140 a DECL node, or an IDENTIFIER_NODE if we do not have a full
141 declaration available. */
142 tree decl;
144 /* Type which will be checked for abstractness. */
145 tree type;
147 /* Kind of use in an unnamed declarator. */
148 abstract_class_use use;
150 /* Position of the declaration. This is only needed for IDENTIFIER_NODEs,
151 because DECLs already carry locus information. */
152 location_t locus;
154 /* Link to the next element in list. */
155 struct pending_abstract_type* next;
158 struct abstract_type_hasher : ggc_hasher<pending_abstract_type *>
160 typedef tree compare_type;
161 static hashval_t hash (pending_abstract_type *);
162 static bool equal (pending_abstract_type *, tree);
165 /* Compute the hash value of the node VAL. This function is used by the
166 hash table abstract_pending_vars. */
168 hashval_t
169 abstract_type_hasher::hash (pending_abstract_type *pat)
171 return (hashval_t) TYPE_UID (pat->type);
175 /* Compare node VAL1 with the type VAL2. This function is used by the
176 hash table abstract_pending_vars. */
178 bool
179 abstract_type_hasher::equal (pending_abstract_type *pat1, tree type2)
181 return (pat1->type == type2);
184 /* Hash table that maintains pending_abstract_type nodes, for which we still
185 need to check for type abstractness. The key of the table is the type
186 of the declaration. */
187 static GTY (()) hash_table<abstract_type_hasher> *abstract_pending_vars = NULL;
189 static int abstract_virtuals_error_sfinae (tree, tree, abstract_class_use, tsubst_flags_t);
191 /* This function is called after TYPE is completed, and will check if there
192 are pending declarations for which we still need to verify the abstractness
193 of TYPE, and emit a diagnostic (through abstract_virtuals_error) if TYPE
194 turned out to be incomplete. */
196 void
197 complete_type_check_abstract (tree type)
199 struct pending_abstract_type *pat;
200 location_t cur_loc = input_location;
202 gcc_assert (COMPLETE_TYPE_P (type));
204 if (!abstract_pending_vars)
205 return;
207 /* Retrieve the list of pending declarations for this type. */
208 pending_abstract_type **slot
209 = abstract_pending_vars->find_slot_with_hash (type, TYPE_UID (type),
210 NO_INSERT);
211 if (!slot)
212 return;
213 pat = *slot;
214 gcc_assert (pat);
216 /* If the type is not abstract, do not do anything. */
217 if (CLASSTYPE_PURE_VIRTUALS (type))
219 struct pending_abstract_type *prev = 0, *next;
221 /* Reverse the list to emit the errors in top-down order. */
222 for (; pat; pat = next)
224 next = pat->next;
225 pat->next = prev;
226 prev = pat;
228 pat = prev;
230 /* Go through the list, and call abstract_virtuals_error for each
231 element: it will issue a diagnostic if the type is abstract. */
232 while (pat)
234 gcc_assert (type == pat->type);
236 /* Tweak input_location so that the diagnostic appears at the correct
237 location. Notice that this is only needed if the decl is an
238 IDENTIFIER_NODE. */
239 input_location = pat->locus;
240 abstract_virtuals_error_sfinae (pat->decl, pat->type, pat->use,
241 tf_warning_or_error);
242 pat = pat->next;
246 abstract_pending_vars->clear_slot (slot);
248 input_location = cur_loc;
252 /* If TYPE has abstract virtual functions, issue an error about trying
253 to create an object of that type. DECL is the object declared, or
254 NULL_TREE if the declaration is unavailable, in which case USE specifies
255 the kind of invalid use. Returns 1 if an error occurred; zero if
256 all was well. */
258 static int
259 abstract_virtuals_error_sfinae (tree decl, tree type, abstract_class_use use,
260 tsubst_flags_t complain)
262 vec<tree, va_gc> *pure;
264 /* This function applies only to classes. Any other entity can never
265 be abstract. */
266 if (!CLASS_TYPE_P (type))
267 return 0;
268 type = TYPE_MAIN_VARIANT (type);
270 #if 0
271 /* Instantiation here seems to be required by the standard,
272 but breaks e.g. boost::bind. FIXME! */
273 /* In SFINAE, non-N3276 context, force instantiation. */
274 if (!(complain & (tf_error|tf_decltype)))
275 complete_type (type);
276 #endif
278 /* If the type is incomplete, we register it within a hash table,
279 so that we can check again once it is completed. This makes sense
280 only for objects for which we have a declaration or at least a
281 name. */
282 if (!COMPLETE_TYPE_P (type) && (complain & tf_error))
284 struct pending_abstract_type *pat;
286 gcc_assert (!decl || DECL_P (decl) || identifier_p (decl));
288 if (!abstract_pending_vars)
289 abstract_pending_vars
290 = hash_table<abstract_type_hasher>::create_ggc (31);
292 pending_abstract_type **slot
293 = abstract_pending_vars->find_slot_with_hash (type, TYPE_UID (type),
294 INSERT);
296 pat = ggc_alloc<pending_abstract_type> ();
297 pat->type = type;
298 pat->decl = decl;
299 pat->use = use;
300 pat->locus = ((decl && DECL_P (decl))
301 ? DECL_SOURCE_LOCATION (decl)
302 : input_location);
304 pat->next = *slot;
305 *slot = pat;
307 return 0;
310 if (!TYPE_SIZE (type))
311 /* TYPE is being defined, and during that time
312 CLASSTYPE_PURE_VIRTUALS holds the inline friends. */
313 return 0;
315 pure = CLASSTYPE_PURE_VIRTUALS (type);
316 if (!pure)
317 return 0;
319 if (!(complain & tf_error))
320 return 1;
322 if (decl)
324 if (VAR_P (decl))
325 error ("cannot declare variable %q+D to be of abstract "
326 "type %qT", decl, type);
327 else if (TREE_CODE (decl) == PARM_DECL)
329 if (DECL_NAME (decl))
330 error ("cannot declare parameter %q+D to be of abstract type %qT",
331 decl, type);
332 else
333 error ("cannot declare parameter to be of abstract type %qT",
334 type);
336 else if (TREE_CODE (decl) == FIELD_DECL)
337 error ("cannot declare field %q+D to be of abstract type %qT",
338 decl, type);
339 else if (TREE_CODE (decl) == FUNCTION_DECL
340 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
341 error ("invalid abstract return type for member function %q+#D", decl);
342 else if (TREE_CODE (decl) == FUNCTION_DECL)
343 error ("invalid abstract return type for function %q+#D", decl);
344 else if (identifier_p (decl))
345 /* Here we do not have location information. */
346 error ("invalid abstract type %qT for %qE", type, decl);
347 else
348 error ("invalid abstract type for %q+D", decl);
350 else switch (use)
352 case ACU_ARRAY:
353 error ("creating array of %qT, which is an abstract class type", type);
354 break;
355 case ACU_CAST:
356 error ("invalid cast to abstract class type %qT", type);
357 break;
358 case ACU_NEW:
359 error ("invalid new-expression of abstract class type %qT", type);
360 break;
361 case ACU_RETURN:
362 error ("invalid abstract return type %qT", type);
363 break;
364 case ACU_PARM:
365 error ("invalid abstract parameter type %qT", type);
366 break;
367 case ACU_THROW:
368 error ("expression of abstract class type %qT cannot "
369 "be used in throw-expression", type);
370 break;
371 case ACU_CATCH:
372 error ("cannot declare catch parameter to be of abstract "
373 "class type %qT", type);
374 break;
375 default:
376 error ("cannot allocate an object of abstract type %qT", type);
379 /* Only go through this once. */
380 if (pure->length ())
382 unsigned ix;
383 tree fn;
385 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
386 " because the following virtual functions are pure within %qT:",
387 type);
389 FOR_EACH_VEC_ELT (*pure, ix, fn)
390 if (! DECL_CLONED_FUNCTION_P (fn)
391 || DECL_COMPLETE_DESTRUCTOR_P (fn))
392 inform (input_location, "\t%+#D", fn);
394 /* Now truncate the vector. This leaves it non-null, so we know
395 there are pure virtuals, but empty so we don't list them out
396 again. */
397 pure->truncate (0);
400 return 1;
404 abstract_virtuals_error_sfinae (tree decl, tree type, tsubst_flags_t complain)
406 return abstract_virtuals_error_sfinae (decl, type, ACU_UNKNOWN, complain);
410 abstract_virtuals_error_sfinae (abstract_class_use use, tree type,
411 tsubst_flags_t complain)
413 return abstract_virtuals_error_sfinae (NULL_TREE, type, use, complain);
417 /* Wrapper for the above function in the common case of wanting errors. */
420 abstract_virtuals_error (tree decl, tree type)
422 return abstract_virtuals_error_sfinae (decl, type, tf_warning_or_error);
426 abstract_virtuals_error (abstract_class_use use, tree type)
428 return abstract_virtuals_error_sfinae (use, type, tf_warning_or_error);
431 /* Print an inform about the declaration of the incomplete type TYPE. */
433 void
434 cxx_incomplete_type_inform (const_tree type)
436 if (!TYPE_MAIN_DECL (type))
437 return;
439 location_t loc = DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type));
440 tree ptype = strip_top_quals (CONST_CAST_TREE (type));
442 if (current_class_type
443 && TYPE_BEING_DEFINED (current_class_type)
444 && same_type_p (ptype, current_class_type))
445 inform (loc, "definition of %q#T is not complete until "
446 "the closing brace", ptype);
447 else if (!TYPE_TEMPLATE_INFO (ptype))
448 inform (loc, "forward declaration of %q#T", ptype);
449 else
450 inform (loc, "declaration of %q#T", ptype);
453 /* Print an error message for invalid use of an incomplete type.
454 VALUE is the expression that was used (or 0 if that isn't known)
455 and TYPE is the type that was invalid. DIAG_KIND indicates the
456 type of diagnostic (see diagnostic.def). */
458 void
459 cxx_incomplete_type_diagnostic (const_tree value, const_tree type,
460 diagnostic_t diag_kind)
462 bool is_decl = false, complained = false;
464 gcc_assert (diag_kind == DK_WARNING
465 || diag_kind == DK_PEDWARN
466 || diag_kind == DK_ERROR);
468 /* Avoid duplicate error message. */
469 if (TREE_CODE (type) == ERROR_MARK)
470 return;
472 if (value != 0 && (VAR_P (value)
473 || TREE_CODE (value) == PARM_DECL
474 || TREE_CODE (value) == FIELD_DECL))
476 complained = emit_diagnostic (diag_kind, input_location, 0,
477 "%q+D has incomplete type", value);
478 is_decl = true;
480 retry:
481 /* We must print an error message. Be clever about what it says. */
483 switch (TREE_CODE (type))
485 case RECORD_TYPE:
486 case UNION_TYPE:
487 case ENUMERAL_TYPE:
488 if (!is_decl)
489 complained = emit_diagnostic (diag_kind, input_location, 0,
490 "invalid use of incomplete type %q#T",
491 type);
492 if (complained)
493 cxx_incomplete_type_inform (type);
494 break;
496 case VOID_TYPE:
497 emit_diagnostic (diag_kind, input_location, 0,
498 "invalid use of %qT", type);
499 break;
501 case ARRAY_TYPE:
502 if (TYPE_DOMAIN (type))
504 type = TREE_TYPE (type);
505 goto retry;
507 emit_diagnostic (diag_kind, input_location, 0,
508 "invalid use of array with unspecified bounds");
509 break;
511 case OFFSET_TYPE:
512 bad_member:
514 tree member = TREE_OPERAND (value, 1);
515 if (is_overloaded_fn (member))
516 member = get_first_fn (member);
517 if (DECL_FUNCTION_MEMBER_P (member)
518 && ! flag_ms_extensions)
519 emit_diagnostic (diag_kind, input_location, 0,
520 "invalid use of member function "
521 "(did you forget the %<()%> ?)");
522 else
523 emit_diagnostic (diag_kind, input_location, 0,
524 "invalid use of member "
525 "(did you forget the %<&%> ?)");
527 break;
529 case TEMPLATE_TYPE_PARM:
530 if (is_auto (type))
531 emit_diagnostic (diag_kind, input_location, 0,
532 "invalid use of %<auto%>");
533 else
534 emit_diagnostic (diag_kind, input_location, 0,
535 "invalid use of template type parameter %qT", type);
536 break;
538 case BOUND_TEMPLATE_TEMPLATE_PARM:
539 emit_diagnostic (diag_kind, input_location, 0,
540 "invalid use of template template parameter %qT",
541 TYPE_NAME (type));
542 break;
544 case TYPENAME_TYPE:
545 emit_diagnostic (diag_kind, input_location, 0,
546 "invalid use of dependent type %qT", type);
547 break;
549 case LANG_TYPE:
550 if (type == init_list_type_node)
552 emit_diagnostic (diag_kind, input_location, 0,
553 "invalid use of brace-enclosed initializer list");
554 break;
556 gcc_assert (type == unknown_type_node);
557 if (value && TREE_CODE (value) == COMPONENT_REF)
558 goto bad_member;
559 else if (value && TREE_CODE (value) == ADDR_EXPR)
560 emit_diagnostic (diag_kind, input_location, 0,
561 "address of overloaded function with no contextual "
562 "type information");
563 else if (value && TREE_CODE (value) == OVERLOAD)
564 emit_diagnostic (diag_kind, input_location, 0,
565 "overloaded function with no contextual type information");
566 else
567 emit_diagnostic (diag_kind, input_location, 0,
568 "insufficient contextual information to determine type");
569 break;
571 default:
572 gcc_unreachable ();
576 /* Backward-compatibility interface to incomplete_type_diagnostic;
577 required by ../tree.c. */
578 #undef cxx_incomplete_type_error
579 void
580 cxx_incomplete_type_error (const_tree value, const_tree type)
582 cxx_incomplete_type_diagnostic (value, type, DK_ERROR);
586 /* The recursive part of split_nonconstant_init. DEST is an lvalue
587 expression to which INIT should be assigned. INIT is a CONSTRUCTOR.
588 Return true if the whole of the value was initialized by the
589 generated statements. */
591 static bool
592 split_nonconstant_init_1 (tree dest, tree init)
594 unsigned HOST_WIDE_INT idx;
595 tree field_index, value;
596 tree type = TREE_TYPE (dest);
597 tree inner_type = NULL;
598 bool array_type_p = false;
599 bool complete_p = true;
600 HOST_WIDE_INT num_split_elts = 0;
602 switch (TREE_CODE (type))
604 case ARRAY_TYPE:
605 inner_type = TREE_TYPE (type);
606 array_type_p = true;
607 /* FALLTHRU */
609 case RECORD_TYPE:
610 case UNION_TYPE:
611 case QUAL_UNION_TYPE:
612 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), idx,
613 field_index, value)
615 /* The current implementation of this algorithm assumes that
616 the field was set for all the elements. This is usually done
617 by process_init_constructor. */
618 gcc_assert (field_index);
620 if (!array_type_p)
621 inner_type = TREE_TYPE (field_index);
623 if (TREE_CODE (value) == CONSTRUCTOR)
625 tree sub;
627 if (array_type_p)
628 sub = build4 (ARRAY_REF, inner_type, dest, field_index,
629 NULL_TREE, NULL_TREE);
630 else
631 sub = build3 (COMPONENT_REF, inner_type, dest, field_index,
632 NULL_TREE);
634 if (!split_nonconstant_init_1 (sub, value))
635 complete_p = false;
636 num_split_elts++;
638 else if (!initializer_constant_valid_p (value, inner_type))
640 tree code;
641 tree sub;
643 /* FIXME: Ordered removal is O(1) so the whole function is
644 worst-case quadratic. This could be fixed using an aside
645 bitmap to record which elements must be removed and remove
646 them all at the same time. Or by merging
647 split_non_constant_init into process_init_constructor_array,
648 that is separating constants from non-constants while building
649 the vector. */
650 CONSTRUCTOR_ELTS (init)->ordered_remove (idx);
651 --idx;
653 if (TREE_CODE (field_index) == RANGE_EXPR)
655 /* Use build_vec_init to initialize a range. */
656 tree low = TREE_OPERAND (field_index, 0);
657 tree hi = TREE_OPERAND (field_index, 1);
658 sub = build4 (ARRAY_REF, inner_type, dest, low,
659 NULL_TREE, NULL_TREE);
660 sub = cp_build_addr_expr (sub, tf_warning_or_error);
661 tree max = size_binop (MINUS_EXPR, hi, low);
662 code = build_vec_init (sub, max, value, false, 0,
663 tf_warning_or_error);
664 add_stmt (code);
665 if (tree_fits_shwi_p (max))
666 num_split_elts += tree_to_shwi (max);
668 else
670 if (array_type_p)
671 sub = build4 (ARRAY_REF, inner_type, dest, field_index,
672 NULL_TREE, NULL_TREE);
673 else
674 sub = build3 (COMPONENT_REF, inner_type, dest, field_index,
675 NULL_TREE);
677 code = build2 (INIT_EXPR, inner_type, sub, value);
678 code = build_stmt (input_location, EXPR_STMT, code);
679 code = maybe_cleanup_point_expr_void (code);
680 add_stmt (code);
681 if (type_build_dtor_call (inner_type))
683 code = (build_special_member_call
684 (sub, complete_dtor_identifier, NULL, inner_type,
685 LOOKUP_NORMAL, tf_warning_or_error));
686 if (!TYPE_HAS_TRIVIAL_DESTRUCTOR (inner_type))
687 finish_eh_cleanup (code);
691 num_split_elts++;
694 break;
696 case VECTOR_TYPE:
697 if (!initializer_constant_valid_p (init, type))
699 tree code;
700 tree cons = copy_node (init);
701 CONSTRUCTOR_ELTS (init) = NULL;
702 code = build2 (MODIFY_EXPR, type, dest, cons);
703 code = build_stmt (input_location, EXPR_STMT, code);
704 add_stmt (code);
705 num_split_elts += CONSTRUCTOR_NELTS (init);
707 break;
709 default:
710 gcc_unreachable ();
713 /* The rest of the initializer is now a constant. */
714 TREE_CONSTANT (init) = 1;
715 return complete_p && complete_ctor_at_level_p (TREE_TYPE (init),
716 num_split_elts, inner_type);
719 /* A subroutine of store_init_value. Splits non-constant static
720 initializer INIT into a constant part and generates code to
721 perform the non-constant part of the initialization to DEST.
722 Returns the code for the runtime init. */
724 static tree
725 split_nonconstant_init (tree dest, tree init)
727 tree code;
729 if (TREE_CODE (init) == CONSTRUCTOR)
731 code = push_stmt_list ();
732 if (split_nonconstant_init_1 (dest, init))
733 init = NULL_TREE;
734 code = pop_stmt_list (code);
735 DECL_INITIAL (dest) = init;
736 TREE_READONLY (dest) = 0;
738 else
739 code = build2 (INIT_EXPR, TREE_TYPE (dest), dest, init);
741 return code;
744 /* Perform appropriate conversions on the initial value of a variable,
745 store it in the declaration DECL,
746 and print any error messages that are appropriate.
747 If the init is invalid, store an ERROR_MARK.
749 C++: Note that INIT might be a TREE_LIST, which would mean that it is
750 a base class initializer for some aggregate type, hopefully compatible
751 with DECL. If INIT is a single element, and DECL is an aggregate
752 type, we silently convert INIT into a TREE_LIST, allowing a constructor
753 to be called.
755 If INIT is a TREE_LIST and there is no constructor, turn INIT
756 into a CONSTRUCTOR and use standard initialization techniques.
757 Perhaps a warning should be generated?
759 Returns code to be executed if initialization could not be performed
760 for static variable. In that case, caller must emit the code. */
762 tree
763 store_init_value (tree decl, tree init, vec<tree, va_gc>** cleanups, int flags)
765 tree value, type;
767 /* If variable's type was invalidly declared, just ignore it. */
769 type = TREE_TYPE (decl);
770 if (TREE_CODE (type) == ERROR_MARK)
771 return NULL_TREE;
773 if (MAYBE_CLASS_TYPE_P (type))
775 if (TREE_CODE (init) == TREE_LIST)
777 error ("constructor syntax used, but no constructor declared "
778 "for type %qT", type);
779 init = build_constructor_from_list (init_list_type_node, nreverse (init));
783 /* End of special C++ code. */
785 if (flags & LOOKUP_ALREADY_DIGESTED)
786 value = init;
787 else
788 /* Digest the specified initializer into an expression. */
789 value = digest_init_flags (type, init, flags);
791 value = extend_ref_init_temps (decl, value, cleanups);
793 /* In C++11 constant expression is a semantic, not syntactic, property.
794 In C++98, make sure that what we thought was a constant expression at
795 template definition time is still constant and otherwise perform this
796 as optimization, e.g. to fold SIZEOF_EXPRs in the initializer. */
797 if (decl_maybe_constant_var_p (decl) || TREE_STATIC (decl))
799 bool const_init;
800 value = instantiate_non_dependent_expr (value);
801 if (DECL_DECLARED_CONSTEXPR_P (decl)
802 || DECL_IN_AGGR_P (decl))
804 /* Diagnose a non-constant initializer for constexpr. */
805 if (processing_template_decl
806 && !require_potential_constant_expression (value))
807 value = error_mark_node;
808 else
809 value = cxx_constant_value (value, decl);
811 value = maybe_constant_init (value, decl);
812 if (TREE_CODE (value) == CONSTRUCTOR && cp_has_mutable_p (type))
813 /* Poison this CONSTRUCTOR so it can't be copied to another
814 constexpr variable. */
815 CONSTRUCTOR_MUTABLE_POISON (value) = true;
816 const_init = (reduced_constant_expression_p (value)
817 || error_operand_p (value));
818 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = const_init;
819 TREE_CONSTANT (decl) = const_init && decl_maybe_constant_var_p (decl);
822 if (cxx_dialect >= cxx14)
823 /* Handle aggregate NSDMI in non-constant initializers, too. */
824 value = replace_placeholders (value, decl);
826 /* If the initializer is not a constant, fill in DECL_INITIAL with
827 the bits that are constant, and then return an expression that
828 will perform the dynamic initialization. */
829 if (value != error_mark_node
830 && (TREE_SIDE_EFFECTS (value)
831 || array_of_runtime_bound_p (type)
832 || ! reduced_constant_expression_p (value)))
834 if (TREE_CODE (type) == ARRAY_TYPE
835 && (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (type))
836 || array_of_runtime_bound_p (type)))
837 /* For an array, we only need/want a single cleanup region rather
838 than one per element. */
839 return build_vec_init (decl, NULL_TREE, value, false, 1,
840 tf_warning_or_error);
841 else
842 return split_nonconstant_init (decl, value);
844 /* If the value is a constant, just put it in DECL_INITIAL. If DECL
845 is an automatic variable, the middle end will turn this into a
846 dynamic initialization later. */
847 DECL_INITIAL (decl) = value;
848 return NULL_TREE;
852 /* Give diagnostic about narrowing conversions within { }. */
854 bool
855 check_narrowing (tree type, tree init, tsubst_flags_t complain)
857 tree ftype = unlowered_expr_type (init);
858 bool ok = true;
859 REAL_VALUE_TYPE d;
861 if (((!warn_narrowing || !(complain & tf_warning))
862 && cxx_dialect == cxx98)
863 || !ARITHMETIC_TYPE_P (type))
864 return ok;
866 if (BRACE_ENCLOSED_INITIALIZER_P (init)
867 && TREE_CODE (type) == COMPLEX_TYPE)
869 tree elttype = TREE_TYPE (type);
870 if (CONSTRUCTOR_NELTS (init) > 0)
871 ok &= check_narrowing (elttype, CONSTRUCTOR_ELT (init, 0)->value,
872 complain);
873 if (CONSTRUCTOR_NELTS (init) > 1)
874 ok &= check_narrowing (elttype, CONSTRUCTOR_ELT (init, 1)->value,
875 complain);
876 return ok;
879 init = fold_non_dependent_expr (init);
881 if (TREE_CODE (type) == INTEGER_TYPE
882 && TREE_CODE (ftype) == REAL_TYPE)
883 ok = false;
884 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype)
885 && CP_INTEGRAL_TYPE_P (type))
887 if (TREE_CODE (ftype) == ENUMERAL_TYPE)
888 /* Check for narrowing based on the values of the enumeration. */
889 ftype = ENUM_UNDERLYING_TYPE (ftype);
890 if ((tree_int_cst_lt (TYPE_MAX_VALUE (type),
891 TYPE_MAX_VALUE (ftype))
892 || tree_int_cst_lt (TYPE_MIN_VALUE (ftype),
893 TYPE_MIN_VALUE (type)))
894 && (TREE_CODE (init) != INTEGER_CST
895 || !int_fits_type_p (init, type)))
896 ok = false;
898 else if (TREE_CODE (ftype) == REAL_TYPE
899 && TREE_CODE (type) == REAL_TYPE)
901 if (TYPE_PRECISION (type) < TYPE_PRECISION (ftype))
903 if (TREE_CODE (init) == REAL_CST)
905 /* Issue 703: Loss of precision is OK as long as the value is
906 within the representable range of the new type. */
907 REAL_VALUE_TYPE r;
908 d = TREE_REAL_CST (init);
909 real_convert (&r, TYPE_MODE (type), &d);
910 if (real_isinf (&r))
911 ok = false;
913 else
914 ok = false;
917 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype)
918 && TREE_CODE (type) == REAL_TYPE)
920 ok = false;
921 if (TREE_CODE (init) == INTEGER_CST)
923 d = real_value_from_int_cst (0, init);
924 if (exact_real_truncate (TYPE_MODE (type), &d))
925 ok = true;
929 if (!ok)
931 if (cxx_dialect == cxx98)
932 warning_at (EXPR_LOC_OR_LOC (init, input_location), OPT_Wnarrowing,
933 "narrowing conversion of %qE from %qT to %qT inside { } "
934 "is ill-formed in C++11", init, ftype, type);
935 else if (!TREE_CONSTANT (init))
937 if (complain & tf_warning_or_error)
939 pedwarn (EXPR_LOC_OR_LOC (init, input_location), OPT_Wnarrowing,
940 "narrowing conversion of %qE from %qT to %qT inside { }",
941 init, ftype, type);
942 ok = true;
945 else if (complain & tf_error)
946 error_at (EXPR_LOC_OR_LOC (init, input_location),
947 "narrowing conversion of %qE from %qT to %qT inside { }",
948 init, ftype, type);
951 return cxx_dialect == cxx98 || ok;
954 /* Process the initializer INIT for a variable of type TYPE, emitting
955 diagnostics for invalid initializers and converting the initializer as
956 appropriate.
958 For aggregate types, it assumes that reshape_init has already run, thus the
959 initializer will have the right shape (brace elision has been undone).
961 NESTED is true iff we are being called for an element of a CONSTRUCTOR. */
963 static tree
964 digest_init_r (tree type, tree init, bool nested, int flags,
965 tsubst_flags_t complain)
967 enum tree_code code = TREE_CODE (type);
969 if (error_operand_p (init))
970 return error_mark_node;
972 gcc_assert (init);
974 /* We must strip the outermost array type when completing the type,
975 because the its bounds might be incomplete at the moment. */
976 if (!complete_type_or_maybe_complain (TREE_CODE (type) == ARRAY_TYPE
977 ? TREE_TYPE (type) : type, NULL_TREE,
978 complain))
979 return error_mark_node;
981 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue
982 (g++.old-deja/g++.law/casts2.C). */
983 if (TREE_CODE (init) == NON_LVALUE_EXPR)
984 init = TREE_OPERAND (init, 0);
986 /* Initialization of an array of chars from a string constant. The initializer
987 can be optionally enclosed in braces, but reshape_init has already removed
988 them if they were present. */
989 if (code == ARRAY_TYPE)
991 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
992 if (char_type_p (typ1)
993 /*&& init */
994 && TREE_CODE (init) == STRING_CST)
996 tree char_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (init)));
998 if (TYPE_PRECISION (typ1) == BITS_PER_UNIT)
1000 if (char_type != char_type_node)
1002 if (complain & tf_error)
1003 error ("char-array initialized from wide string");
1004 return error_mark_node;
1007 else
1009 if (char_type == char_type_node)
1011 if (complain & tf_error)
1012 error ("int-array initialized from non-wide string");
1013 return error_mark_node;
1015 else if (char_type != typ1)
1017 if (complain & tf_error)
1018 error ("int-array initialized from incompatible "
1019 "wide string");
1020 return error_mark_node;
1024 if (type != TREE_TYPE (init))
1026 init = copy_node (init);
1027 TREE_TYPE (init) = type;
1029 if (TYPE_DOMAIN (type) != 0 && TREE_CONSTANT (TYPE_SIZE (type)))
1031 int size = TREE_INT_CST_LOW (TYPE_SIZE (type));
1032 size = (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
1033 /* In C it is ok to subtract 1 from the length of the string
1034 because it's ok to ignore the terminating null char that is
1035 counted in the length of the constant, but in C++ this would
1036 be invalid. */
1037 if (size < TREE_STRING_LENGTH (init))
1038 permerror (input_location, "initializer-string for array "
1039 "of chars is too long");
1041 return init;
1045 /* Handle scalar types (including conversions) and references. */
1046 if ((TREE_CODE (type) != COMPLEX_TYPE
1047 || BRACE_ENCLOSED_INITIALIZER_P (init))
1048 && (SCALAR_TYPE_P (type) || code == REFERENCE_TYPE))
1050 tree *exp;
1052 if (nested)
1053 flags |= LOOKUP_NO_NARROWING;
1054 init = convert_for_initialization (0, type, init, flags,
1055 ICR_INIT, NULL_TREE, 0,
1056 complain);
1057 exp = &init;
1059 /* Skip any conversions since we'll be outputting the underlying
1060 constant. */
1061 while (CONVERT_EXPR_P (*exp)
1062 || TREE_CODE (*exp) == NON_LVALUE_EXPR)
1063 exp = &TREE_OPERAND (*exp, 0);
1065 *exp = cplus_expand_constant (*exp);
1067 return init;
1070 /* Come here only for aggregates: records, arrays, unions, complex numbers
1071 and vectors. */
1072 gcc_assert (TREE_CODE (type) == ARRAY_TYPE
1073 || TREE_CODE (type) == VECTOR_TYPE
1074 || TREE_CODE (type) == RECORD_TYPE
1075 || TREE_CODE (type) == UNION_TYPE
1076 || TREE_CODE (type) == COMPLEX_TYPE);
1078 if (BRACE_ENCLOSED_INITIALIZER_P (init)
1079 && !TYPE_NON_AGGREGATE_CLASS (type))
1080 return process_init_constructor (type, init, complain);
1081 else
1083 if (COMPOUND_LITERAL_P (init) && TREE_CODE (type) == ARRAY_TYPE)
1085 if (complain & tf_error)
1086 error ("cannot initialize aggregate of type %qT with "
1087 "a compound literal", type);
1089 return error_mark_node;
1092 if (TREE_CODE (type) == ARRAY_TYPE
1093 && !BRACE_ENCLOSED_INITIALIZER_P (init))
1095 /* Allow the result of build_array_copy and of
1096 build_value_init_noctor. */
1097 if ((TREE_CODE (init) == VEC_INIT_EXPR
1098 || TREE_CODE (init) == CONSTRUCTOR)
1099 && (same_type_ignoring_top_level_qualifiers_p
1100 (type, TREE_TYPE (init))))
1101 return init;
1103 if (complain & tf_error)
1104 error ("array must be initialized with a brace-enclosed"
1105 " initializer");
1106 return error_mark_node;
1109 return convert_for_initialization (NULL_TREE, type, init,
1110 flags,
1111 ICR_INIT, NULL_TREE, 0,
1112 complain);
1116 tree
1117 digest_init (tree type, tree init, tsubst_flags_t complain)
1119 return digest_init_r (type, init, false, LOOKUP_IMPLICIT, complain);
1122 tree
1123 digest_init_flags (tree type, tree init, int flags)
1125 return digest_init_r (type, init, false, flags, tf_warning_or_error);
1128 /* Process the initializer INIT for an NSDMI DECL (a FIELD_DECL). */
1129 tree
1130 digest_nsdmi_init (tree decl, tree init)
1132 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
1134 int flags = LOOKUP_IMPLICIT;
1135 if (DIRECT_LIST_INIT_P (init))
1136 flags = LOOKUP_NORMAL;
1137 init = digest_init_flags (TREE_TYPE (decl), init, flags);
1138 if (TREE_CODE (init) == TARGET_EXPR)
1139 /* This represents the whole initialization. */
1140 TARGET_EXPR_DIRECT_INIT_P (init) = true;
1141 return init;
1144 /* Set of flags used within process_init_constructor to describe the
1145 initializers. */
1146 #define PICFLAG_ERRONEOUS 1
1147 #define PICFLAG_NOT_ALL_CONSTANT 2
1148 #define PICFLAG_NOT_ALL_SIMPLE 4
1149 #define PICFLAG_SIDE_EFFECTS 8
1151 /* Given an initializer INIT, return the flag (PICFLAG_*) which better
1152 describe it. */
1154 static int
1155 picflag_from_initializer (tree init)
1157 if (init == error_mark_node)
1158 return PICFLAG_ERRONEOUS;
1159 else if (!TREE_CONSTANT (init))
1161 if (TREE_SIDE_EFFECTS (init))
1162 return PICFLAG_SIDE_EFFECTS;
1163 else
1164 return PICFLAG_NOT_ALL_CONSTANT;
1166 else if (!initializer_constant_valid_p (init, TREE_TYPE (init)))
1167 return PICFLAG_NOT_ALL_SIMPLE;
1168 return 0;
1171 /* Adjust INIT for going into a CONSTRUCTOR. */
1173 static tree
1174 massage_init_elt (tree type, tree init, tsubst_flags_t complain)
1176 init = digest_init_r (type, init, true, LOOKUP_IMPLICIT, complain);
1177 /* Strip a simple TARGET_EXPR when we know this is an initializer. */
1178 if (TREE_CODE (init) == TARGET_EXPR
1179 && !VOID_TYPE_P (TREE_TYPE (TARGET_EXPR_INITIAL (init))))
1180 init = TARGET_EXPR_INITIAL (init);
1181 /* When we defer constant folding within a statement, we may want to
1182 defer this folding as well. */
1183 tree t = fold_non_dependent_expr (init);
1184 t = maybe_constant_init (t);
1185 if (TREE_CONSTANT (t))
1186 init = t;
1187 return init;
1190 /* Subroutine of process_init_constructor, which will process an initializer
1191 INIT for an array or vector of type TYPE. Returns the flags (PICFLAG_*)
1192 which describe the initializers. */
1194 static int
1195 process_init_constructor_array (tree type, tree init,
1196 tsubst_flags_t complain)
1198 unsigned HOST_WIDE_INT i, len = 0;
1199 int flags = 0;
1200 bool unbounded = false;
1201 constructor_elt *ce;
1202 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (init);
1204 gcc_assert (TREE_CODE (type) == ARRAY_TYPE
1205 || TREE_CODE (type) == VECTOR_TYPE);
1207 if (TREE_CODE (type) == ARRAY_TYPE)
1209 tree domain = TYPE_DOMAIN (type);
1210 if (domain && TREE_CONSTANT (TYPE_MAX_VALUE (domain)))
1211 len = wi::ext (wi::to_offset (TYPE_MAX_VALUE (domain))
1212 - wi::to_offset (TYPE_MIN_VALUE (domain)) + 1,
1213 TYPE_PRECISION (TREE_TYPE (domain)),
1214 TYPE_SIGN (TREE_TYPE (domain))).to_uhwi ();
1215 else
1216 unbounded = true; /* Take as many as there are. */
1218 else
1219 /* Vectors are like simple fixed-size arrays. */
1220 len = TYPE_VECTOR_SUBPARTS (type);
1222 /* There must not be more initializers than needed. */
1223 if (!unbounded && vec_safe_length (v) > len)
1225 if (complain & tf_error)
1226 error ("too many initializers for %qT", type);
1227 else
1228 return PICFLAG_ERRONEOUS;
1231 FOR_EACH_VEC_SAFE_ELT (v, i, ce)
1233 if (ce->index)
1235 gcc_assert (TREE_CODE (ce->index) == INTEGER_CST);
1236 if (compare_tree_int (ce->index, i) != 0)
1238 ce->value = error_mark_node;
1239 sorry ("non-trivial designated initializers not supported");
1242 else
1243 ce->index = size_int (i);
1244 gcc_assert (ce->value);
1245 ce->value = massage_init_elt (TREE_TYPE (type), ce->value, complain);
1247 if (ce->value != error_mark_node)
1248 gcc_assert (same_type_ignoring_top_level_qualifiers_p
1249 (TREE_TYPE (type), TREE_TYPE (ce->value)));
1251 flags |= picflag_from_initializer (ce->value);
1254 /* No more initializers. If the array is unbounded, we are done. Otherwise,
1255 we must add initializers ourselves. */
1256 if (!unbounded)
1257 for (; i < len; ++i)
1259 tree next;
1261 if (type_build_ctor_call (TREE_TYPE (type)))
1263 /* If this type needs constructors run for default-initialization,
1264 we can't rely on the back end to do it for us, so make the
1265 initialization explicit by list-initializing from T{}. */
1266 next = build_constructor (init_list_type_node, NULL);
1267 CONSTRUCTOR_IS_DIRECT_INIT (next) = true;
1268 next = massage_init_elt (TREE_TYPE (type), next, complain);
1269 if (initializer_zerop (next))
1270 /* The default zero-initialization is fine for us; don't
1271 add anything to the CONSTRUCTOR. */
1272 next = NULL_TREE;
1274 else if (!zero_init_p (TREE_TYPE (type)))
1275 next = build_zero_init (TREE_TYPE (type),
1276 /*nelts=*/NULL_TREE,
1277 /*static_storage_p=*/false);
1278 else
1279 /* The default zero-initialization is fine for us; don't
1280 add anything to the CONSTRUCTOR. */
1281 next = NULL_TREE;
1283 if (next)
1285 flags |= picflag_from_initializer (next);
1286 CONSTRUCTOR_APPEND_ELT (v, size_int (i), next);
1290 CONSTRUCTOR_ELTS (init) = v;
1291 return flags;
1294 /* Subroutine of process_init_constructor, which will process an initializer
1295 INIT for a class of type TYPE. Returns the flags (PICFLAG_*) which describe
1296 the initializers. */
1298 static int
1299 process_init_constructor_record (tree type, tree init,
1300 tsubst_flags_t complain)
1302 vec<constructor_elt, va_gc> *v = NULL;
1303 tree field;
1304 int skipped = 0;
1306 gcc_assert (TREE_CODE (type) == RECORD_TYPE);
1307 gcc_assert (!CLASSTYPE_VBASECLASSES (type));
1308 gcc_assert (!TYPE_BINFO (type)
1309 || !BINFO_N_BASE_BINFOS (TYPE_BINFO (type)));
1310 gcc_assert (!TYPE_POLYMORPHIC_P (type));
1312 restart:
1313 int flags = 0;
1314 unsigned HOST_WIDE_INT idx = 0;
1315 /* Generally, we will always have an index for each initializer (which is
1316 a FIELD_DECL, put by reshape_init), but compound literals don't go trough
1317 reshape_init. So we need to handle both cases. */
1318 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
1320 tree next;
1321 tree type;
1323 if (!DECL_NAME (field) && DECL_C_BIT_FIELD (field))
1324 continue;
1326 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
1327 continue;
1329 /* If this is a bitfield, first convert to the declared type. */
1330 type = TREE_TYPE (field);
1331 if (DECL_BIT_FIELD_TYPE (field))
1332 type = DECL_BIT_FIELD_TYPE (field);
1333 if (type == error_mark_node)
1334 return PICFLAG_ERRONEOUS;
1336 if (idx < vec_safe_length (CONSTRUCTOR_ELTS (init)))
1338 constructor_elt *ce = &(*CONSTRUCTOR_ELTS (init))[idx];
1339 if (ce->index)
1341 /* We can have either a FIELD_DECL or an IDENTIFIER_NODE. The
1342 latter case can happen in templates where lookup has to be
1343 deferred. */
1344 gcc_assert (TREE_CODE (ce->index) == FIELD_DECL
1345 || identifier_p (ce->index));
1346 if (ce->index != field
1347 && ce->index != DECL_NAME (field))
1349 ce->value = error_mark_node;
1350 sorry ("non-trivial designated initializers not supported");
1354 gcc_assert (ce->value);
1355 next = massage_init_elt (type, ce->value, complain);
1356 ++idx;
1358 else if (DECL_INITIAL (field))
1360 if (skipped > 0)
1362 /* We're using an NSDMI past a field with implicit
1363 zero-init. Go back and make it explicit. */
1364 skipped = -1;
1365 vec_safe_truncate (v, 0);
1366 goto restart;
1368 /* C++14 aggregate NSDMI. */
1369 next = get_nsdmi (field, /*ctor*/false);
1371 else if (type_build_ctor_call (TREE_TYPE (field)))
1373 /* If this type needs constructors run for
1374 default-initialization, we can't rely on the back end to do it
1375 for us, so build up TARGET_EXPRs. If the type in question is
1376 a class, just build one up; if it's an array, recurse. */
1377 next = build_constructor (init_list_type_node, NULL);
1378 /* Call this direct-initialization pending DR 1518 resolution so
1379 that explicit default ctors don't break valid C++03 code. */
1380 CONSTRUCTOR_IS_DIRECT_INIT (next) = true;
1381 next = massage_init_elt (TREE_TYPE (field), next, complain);
1383 /* Warn when some struct elements are implicitly initialized. */
1384 if ((complain & tf_warning)
1385 && !EMPTY_CONSTRUCTOR_P (init))
1386 warning (OPT_Wmissing_field_initializers,
1387 "missing initializer for member %qD", field);
1389 else
1391 if (TREE_CODE (TREE_TYPE (field)) == REFERENCE_TYPE)
1393 if (complain & tf_error)
1394 error ("member %qD is uninitialized reference", field);
1395 else
1396 return PICFLAG_ERRONEOUS;
1398 else if (CLASSTYPE_REF_FIELDS_NEED_INIT (TREE_TYPE (field)))
1400 if (complain & tf_error)
1401 error ("member %qD with uninitialized reference fields", field);
1402 else
1403 return PICFLAG_ERRONEOUS;
1406 /* Warn when some struct elements are implicitly initialized
1407 to zero. */
1408 if ((complain & tf_warning)
1409 && !EMPTY_CONSTRUCTOR_P (init))
1410 warning (OPT_Wmissing_field_initializers,
1411 "missing initializer for member %qD", field);
1413 if (!zero_init_p (TREE_TYPE (field))
1414 || skipped < 0)
1415 next = build_zero_init (TREE_TYPE (field), /*nelts=*/NULL_TREE,
1416 /*static_storage_p=*/false);
1417 else
1419 /* The default zero-initialization is fine for us; don't
1420 add anything to the CONSTRUCTOR. */
1421 skipped = 1;
1422 continue;
1426 /* If this is a bitfield, now convert to the lowered type. */
1427 if (type != TREE_TYPE (field))
1428 next = cp_convert_and_check (TREE_TYPE (field), next, complain);
1429 flags |= picflag_from_initializer (next);
1430 CONSTRUCTOR_APPEND_ELT (v, field, next);
1433 if (idx < vec_safe_length (CONSTRUCTOR_ELTS (init)))
1435 if (complain & tf_error)
1436 error ("too many initializers for %qT", type);
1437 else
1438 return PICFLAG_ERRONEOUS;
1441 CONSTRUCTOR_ELTS (init) = v;
1442 return flags;
1445 /* Subroutine of process_init_constructor, which will process a single
1446 initializer INIT for a union of type TYPE. Returns the flags (PICFLAG_*)
1447 which describe the initializer. */
1449 static int
1450 process_init_constructor_union (tree type, tree init,
1451 tsubst_flags_t complain)
1453 constructor_elt *ce;
1454 int len;
1456 /* If the initializer was empty, use default zero initialization. */
1457 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
1458 return 0;
1460 len = CONSTRUCTOR_ELTS (init)->length ();
1461 if (len > 1)
1463 if (!(complain & tf_error))
1464 return PICFLAG_ERRONEOUS;
1465 error ("too many initializers for %qT", type);
1466 CONSTRUCTOR_ELTS (init)->block_remove (1, len-1);
1469 ce = &(*CONSTRUCTOR_ELTS (init))[0];
1471 /* If this element specifies a field, initialize via that field. */
1472 if (ce->index)
1474 if (TREE_CODE (ce->index) == FIELD_DECL)
1476 else if (identifier_p (ce->index))
1478 /* This can happen within a cast, see g++.dg/opt/cse2.C. */
1479 tree name = ce->index;
1480 tree field;
1481 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1482 if (DECL_NAME (field) == name)
1483 break;
1484 if (!field)
1486 if (complain & tf_error)
1487 error ("no field %qD found in union being initialized",
1488 field);
1489 ce->value = error_mark_node;
1491 ce->index = field;
1493 else
1495 gcc_assert (TREE_CODE (ce->index) == INTEGER_CST
1496 || TREE_CODE (ce->index) == RANGE_EXPR);
1497 if (complain & tf_error)
1498 error ("index value instead of field name in union initializer");
1499 ce->value = error_mark_node;
1502 else
1504 /* Find the first named field. ANSI decided in September 1990
1505 that only named fields count here. */
1506 tree field = TYPE_FIELDS (type);
1507 while (field && (!DECL_NAME (field) || TREE_CODE (field) != FIELD_DECL))
1508 field = TREE_CHAIN (field);
1509 if (field == NULL_TREE)
1511 if (complain & tf_error)
1512 error ("too many initializers for %qT", type);
1513 ce->value = error_mark_node;
1515 ce->index = field;
1518 if (ce->value && ce->value != error_mark_node)
1519 ce->value = massage_init_elt (TREE_TYPE (ce->index), ce->value, complain);
1521 return picflag_from_initializer (ce->value);
1524 /* Process INIT, a constructor for a variable of aggregate type TYPE. The
1525 constructor is a brace-enclosed initializer, and will be modified in-place.
1527 Each element is converted to the right type through digest_init, and
1528 missing initializers are added following the language rules (zero-padding,
1529 etc.).
1531 After the execution, the initializer will have TREE_CONSTANT if all elts are
1532 constant, and TREE_STATIC set if, in addition, all elts are simple enough
1533 constants that the assembler and linker can compute them.
1535 The function returns the initializer itself, or error_mark_node in case
1536 of error. */
1538 static tree
1539 process_init_constructor (tree type, tree init, tsubst_flags_t complain)
1541 int flags;
1543 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
1545 if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == VECTOR_TYPE)
1546 flags = process_init_constructor_array (type, init, complain);
1547 else if (TREE_CODE (type) == RECORD_TYPE)
1548 flags = process_init_constructor_record (type, init, complain);
1549 else if (TREE_CODE (type) == UNION_TYPE)
1550 flags = process_init_constructor_union (type, init, complain);
1551 else
1552 gcc_unreachable ();
1554 if (flags & PICFLAG_ERRONEOUS)
1555 return error_mark_node;
1557 TREE_TYPE (init) = type;
1558 if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == NULL_TREE)
1559 cp_complete_array_type (&TREE_TYPE (init), init, /*do_default=*/0);
1560 if (flags & PICFLAG_SIDE_EFFECTS)
1562 TREE_CONSTANT (init) = false;
1563 TREE_SIDE_EFFECTS (init) = true;
1565 else if (flags & PICFLAG_NOT_ALL_CONSTANT)
1566 /* Make sure TREE_CONSTANT isn't set from build_constructor. */
1567 TREE_CONSTANT (init) = false;
1568 else
1570 TREE_CONSTANT (init) = 1;
1571 if (!(flags & PICFLAG_NOT_ALL_SIMPLE))
1572 TREE_STATIC (init) = 1;
1574 return init;
1577 /* Given a structure or union value DATUM, construct and return
1578 the structure or union component which results from narrowing
1579 that value to the base specified in BASETYPE. For example, given the
1580 hierarchy
1582 class L { int ii; };
1583 class A : L { ... };
1584 class B : L { ... };
1585 class C : A, B { ... };
1587 and the declaration
1589 C x;
1591 then the expression
1593 x.A::ii refers to the ii member of the L part of
1594 the A part of the C object named by X. In this case,
1595 DATUM would be x, and BASETYPE would be A.
1597 I used to think that this was nonconformant, that the standard specified
1598 that first we look up ii in A, then convert x to an L& and pull out the
1599 ii part. But in fact, it does say that we convert x to an A&; A here
1600 is known as the "naming class". (jason 2000-12-19)
1602 BINFO_P points to a variable initialized either to NULL_TREE or to the
1603 binfo for the specific base subobject we want to convert to. */
1605 tree
1606 build_scoped_ref (tree datum, tree basetype, tree* binfo_p)
1608 tree binfo;
1610 if (datum == error_mark_node)
1611 return error_mark_node;
1612 if (*binfo_p)
1613 binfo = *binfo_p;
1614 else
1615 binfo = lookup_base (TREE_TYPE (datum), basetype, ba_check,
1616 NULL, tf_warning_or_error);
1618 if (!binfo || binfo == error_mark_node)
1620 *binfo_p = NULL_TREE;
1621 if (!binfo)
1622 error_not_base_type (basetype, TREE_TYPE (datum));
1623 return error_mark_node;
1626 *binfo_p = binfo;
1627 return build_base_path (PLUS_EXPR, datum, binfo, 1,
1628 tf_warning_or_error);
1631 /* Build a reference to an object specified by the C++ `->' operator.
1632 Usually this just involves dereferencing the object, but if the
1633 `->' operator is overloaded, then such overloads must be
1634 performed until an object which does not have the `->' operator
1635 overloaded is found. An error is reported when circular pointer
1636 delegation is detected. */
1638 tree
1639 build_x_arrow (location_t loc, tree expr, tsubst_flags_t complain)
1641 tree orig_expr = expr;
1642 tree type = TREE_TYPE (expr);
1643 tree last_rval = NULL_TREE;
1644 vec<tree, va_gc> *types_memoized = NULL;
1646 if (type == error_mark_node)
1647 return error_mark_node;
1649 if (processing_template_decl)
1651 if (type_dependent_expression_p (expr))
1652 return build_min_nt_loc (loc, ARROW_EXPR, expr);
1653 expr = build_non_dependent_expr (expr);
1656 if (MAYBE_CLASS_TYPE_P (type))
1658 struct tinst_level *actual_inst = current_instantiation ();
1659 tree fn = NULL;
1661 while ((expr = build_new_op (loc, COMPONENT_REF,
1662 LOOKUP_NORMAL, expr, NULL_TREE, NULL_TREE,
1663 &fn, complain)))
1665 if (expr == error_mark_node)
1666 return error_mark_node;
1668 /* This provides a better instantiation backtrace in case of
1669 error. */
1670 if (fn && DECL_USE_TEMPLATE (fn))
1671 push_tinst_level_loc (fn,
1672 (current_instantiation () != actual_inst)
1673 ? DECL_SOURCE_LOCATION (fn)
1674 : input_location);
1675 fn = NULL;
1677 if (vec_member (TREE_TYPE (expr), types_memoized))
1679 if (complain & tf_error)
1680 error ("circular pointer delegation detected");
1681 return error_mark_node;
1684 vec_safe_push (types_memoized, TREE_TYPE (expr));
1685 last_rval = expr;
1688 while (current_instantiation () != actual_inst)
1689 pop_tinst_level ();
1691 if (last_rval == NULL_TREE)
1693 if (complain & tf_error)
1694 error ("base operand of %<->%> has non-pointer type %qT", type);
1695 return error_mark_node;
1698 if (TREE_CODE (TREE_TYPE (last_rval)) == REFERENCE_TYPE)
1699 last_rval = convert_from_reference (last_rval);
1701 else
1702 last_rval = decay_conversion (expr, complain);
1704 if (TYPE_PTR_P (TREE_TYPE (last_rval)))
1706 if (processing_template_decl)
1708 expr = build_min (ARROW_EXPR, TREE_TYPE (TREE_TYPE (last_rval)),
1709 orig_expr);
1710 TREE_SIDE_EFFECTS (expr) = TREE_SIDE_EFFECTS (last_rval);
1711 return expr;
1714 return cp_build_indirect_ref (last_rval, RO_NULL, complain);
1717 if (complain & tf_error)
1719 if (types_memoized)
1720 error ("result of %<operator->()%> yields non-pointer result");
1721 else
1722 error ("base operand of %<->%> is not a pointer");
1724 return error_mark_node;
1727 /* Return an expression for "DATUM .* COMPONENT". DATUM has not
1728 already been checked out to be of aggregate type. */
1730 tree
1731 build_m_component_ref (tree datum, tree component, tsubst_flags_t complain)
1733 tree ptrmem_type;
1734 tree objtype;
1735 tree type;
1736 tree binfo;
1737 tree ctype;
1739 if (error_operand_p (datum) || error_operand_p (component))
1740 return error_mark_node;
1742 datum = mark_lvalue_use (datum);
1743 component = mark_rvalue_use (component);
1745 ptrmem_type = TREE_TYPE (component);
1746 if (!TYPE_PTRMEM_P (ptrmem_type))
1748 if (complain & tf_error)
1749 error ("%qE cannot be used as a member pointer, since it is of "
1750 "type %qT", component, ptrmem_type);
1751 return error_mark_node;
1754 objtype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
1755 if (! MAYBE_CLASS_TYPE_P (objtype))
1757 if (complain & tf_error)
1758 error ("cannot apply member pointer %qE to %qE, which is of "
1759 "non-class type %qT", component, datum, objtype);
1760 return error_mark_node;
1763 type = TYPE_PTRMEM_POINTED_TO_TYPE (ptrmem_type);
1764 ctype = complete_type (TYPE_PTRMEM_CLASS_TYPE (ptrmem_type));
1766 if (!COMPLETE_TYPE_P (ctype))
1768 if (!same_type_p (ctype, objtype))
1769 goto mismatch;
1770 binfo = NULL;
1772 else
1774 binfo = lookup_base (objtype, ctype, ba_check, NULL, complain);
1776 if (!binfo)
1778 mismatch:
1779 if (complain & tf_error)
1780 error ("pointer to member type %qT incompatible with object "
1781 "type %qT", type, objtype);
1782 return error_mark_node;
1784 else if (binfo == error_mark_node)
1785 return error_mark_node;
1788 if (TYPE_PTRDATAMEM_P (ptrmem_type))
1790 cp_lvalue_kind kind = lvalue_kind (datum);
1791 tree ptype;
1793 /* Compute the type of the field, as described in [expr.ref].
1794 There's no such thing as a mutable pointer-to-member, so
1795 things are not as complex as they are for references to
1796 non-static data members. */
1797 type = cp_build_qualified_type (type,
1798 (cp_type_quals (type)
1799 | cp_type_quals (TREE_TYPE (datum))));
1801 datum = build_address (datum);
1803 /* Convert object to the correct base. */
1804 if (binfo)
1806 datum = build_base_path (PLUS_EXPR, datum, binfo, 1, complain);
1807 if (datum == error_mark_node)
1808 return error_mark_node;
1811 /* Build an expression for "object + offset" where offset is the
1812 value stored in the pointer-to-data-member. */
1813 ptype = build_pointer_type (type);
1814 datum = fold_build_pointer_plus (fold_convert (ptype, datum), component);
1815 datum = cp_build_indirect_ref (datum, RO_NULL, complain);
1816 if (datum == error_mark_node)
1817 return error_mark_node;
1819 /* If the object expression was an rvalue, return an rvalue. */
1820 if (kind & clk_class)
1821 datum = rvalue (datum);
1822 else if (kind & clk_rvalueref)
1823 datum = move (datum);
1824 return datum;
1826 else
1828 /* 5.5/6: In a .* expression whose object expression is an rvalue, the
1829 program is ill-formed if the second operand is a pointer to member
1830 function with ref-qualifier &. In a .* expression whose object
1831 expression is an lvalue, the program is ill-formed if the second
1832 operand is a pointer to member function with ref-qualifier &&. */
1833 if (FUNCTION_REF_QUALIFIED (type))
1835 bool lval = real_lvalue_p (datum);
1836 if (lval && FUNCTION_RVALUE_QUALIFIED (type))
1838 if (complain & tf_error)
1839 error ("pointer-to-member-function type %qT requires an rvalue",
1840 ptrmem_type);
1841 return error_mark_node;
1843 else if (!lval && !FUNCTION_RVALUE_QUALIFIED (type))
1845 if (complain & tf_error)
1846 error ("pointer-to-member-function type %qT requires an lvalue",
1847 ptrmem_type);
1848 return error_mark_node;
1851 return build2 (OFFSET_REF, type, datum, component);
1855 /* Return a tree node for the expression TYPENAME '(' PARMS ')'. */
1857 tree
1858 build_functional_cast (tree exp, tree parms, tsubst_flags_t complain)
1860 /* This is either a call to a constructor,
1861 or a C cast in C++'s `functional' notation. */
1863 /* The type to which we are casting. */
1864 tree type;
1865 vec<tree, va_gc> *parmvec;
1867 if (error_operand_p (exp) || parms == error_mark_node)
1868 return error_mark_node;
1870 if (TREE_CODE (exp) == TYPE_DECL)
1872 type = TREE_TYPE (exp);
1874 if (complain & tf_warning
1875 && TREE_DEPRECATED (type)
1876 && DECL_ARTIFICIAL (exp))
1877 warn_deprecated_use (type, NULL_TREE);
1879 else
1880 type = exp;
1882 /* We need to check this explicitly, since value-initialization of
1883 arrays is allowed in other situations. */
1884 if (TREE_CODE (type) == ARRAY_TYPE)
1886 if (complain & tf_error)
1887 error ("functional cast to array type %qT", type);
1888 return error_mark_node;
1891 if (type_uses_auto (type))
1893 if (complain & tf_error)
1894 error ("invalid use of %<auto%>");
1895 return error_mark_node;
1898 if (processing_template_decl)
1900 tree t;
1902 /* Diagnose this even in a template. We could also try harder
1903 to give all the usual errors when the type and args are
1904 non-dependent... */
1905 if (TREE_CODE (type) == REFERENCE_TYPE && !parms)
1907 if (complain & tf_error)
1908 error ("invalid value-initialization of reference type");
1909 return error_mark_node;
1912 t = build_min (CAST_EXPR, type, parms);
1913 /* We don't know if it will or will not have side effects. */
1914 TREE_SIDE_EFFECTS (t) = 1;
1915 return t;
1918 if (! MAYBE_CLASS_TYPE_P (type))
1920 if (parms == NULL_TREE)
1922 if (VOID_TYPE_P (type))
1923 return void_node;
1924 return build_value_init (cv_unqualified (type), complain);
1927 /* This must build a C cast. */
1928 parms = build_x_compound_expr_from_list (parms, ELK_FUNC_CAST, complain);
1929 return cp_build_c_cast (type, parms, complain);
1932 /* Prepare to evaluate as a call to a constructor. If this expression
1933 is actually used, for example,
1935 return X (arg1, arg2, ...);
1937 then the slot being initialized will be filled in. */
1939 if (!complete_type_or_maybe_complain (type, NULL_TREE, complain))
1940 return error_mark_node;
1941 if (abstract_virtuals_error_sfinae (ACU_CAST, type, complain))
1942 return error_mark_node;
1944 /* [expr.type.conv]
1946 If the expression list is a single-expression, the type
1947 conversion is equivalent (in definedness, and if defined in
1948 meaning) to the corresponding cast expression. */
1949 if (parms && TREE_CHAIN (parms) == NULL_TREE)
1950 return cp_build_c_cast (type, TREE_VALUE (parms), complain);
1952 /* [expr.type.conv]
1954 The expression T(), where T is a simple-type-specifier for a
1955 non-array complete object type or the (possibly cv-qualified)
1956 void type, creates an rvalue of the specified type, which is
1957 value-initialized. */
1959 if (parms == NULL_TREE)
1961 exp = build_value_init (type, complain);
1962 exp = get_target_expr_sfinae (exp, complain);
1963 return exp;
1966 /* Call the constructor. */
1967 parmvec = make_tree_vector ();
1968 for (; parms != NULL_TREE; parms = TREE_CHAIN (parms))
1969 vec_safe_push (parmvec, TREE_VALUE (parms));
1970 exp = build_special_member_call (NULL_TREE, complete_ctor_identifier,
1971 &parmvec, type, LOOKUP_NORMAL, complain);
1972 release_tree_vector (parmvec);
1974 if (exp == error_mark_node)
1975 return error_mark_node;
1977 return build_cplus_new (type, exp, complain);
1981 /* Add new exception specifier SPEC, to the LIST we currently have.
1982 If it's already in LIST then do nothing.
1983 Moan if it's bad and we're allowed to. COMPLAIN < 0 means we
1984 know what we're doing. */
1986 tree
1987 add_exception_specifier (tree list, tree spec, int complain)
1989 bool ok;
1990 tree core = spec;
1991 bool is_ptr;
1992 diagnostic_t diag_type = DK_UNSPECIFIED; /* none */
1994 if (spec == error_mark_node)
1995 return list;
1997 gcc_assert (spec && (!list || TREE_VALUE (list)));
1999 /* [except.spec] 1, type in an exception specifier shall not be
2000 incomplete, or pointer or ref to incomplete other than pointer
2001 to cv void. */
2002 is_ptr = TYPE_PTR_P (core);
2003 if (is_ptr || TREE_CODE (core) == REFERENCE_TYPE)
2004 core = TREE_TYPE (core);
2005 if (complain < 0)
2006 ok = true;
2007 else if (VOID_TYPE_P (core))
2008 ok = is_ptr;
2009 else if (TREE_CODE (core) == TEMPLATE_TYPE_PARM)
2010 ok = true;
2011 else if (processing_template_decl)
2012 ok = true;
2013 else
2015 ok = true;
2016 /* 15.4/1 says that types in an exception specifier must be complete,
2017 but it seems more reasonable to only require this on definitions
2018 and calls. So just give a pedwarn at this point; we will give an
2019 error later if we hit one of those two cases. */
2020 if (!COMPLETE_TYPE_P (complete_type (core)))
2021 diag_type = DK_PEDWARN; /* pedwarn */
2024 if (ok)
2026 tree probe;
2028 for (probe = list; probe; probe = TREE_CHAIN (probe))
2029 if (same_type_p (TREE_VALUE (probe), spec))
2030 break;
2031 if (!probe)
2032 list = tree_cons (NULL_TREE, spec, list);
2034 else
2035 diag_type = DK_ERROR; /* error */
2037 if (diag_type != DK_UNSPECIFIED
2038 && (complain & tf_warning_or_error))
2039 cxx_incomplete_type_diagnostic (NULL_TREE, core, diag_type);
2041 return list;
2044 /* Like nothrow_spec_p, but don't abort on deferred noexcept. */
2046 static bool
2047 nothrow_spec_p_uninst (const_tree spec)
2049 if (DEFERRED_NOEXCEPT_SPEC_P (spec))
2050 return false;
2051 return nothrow_spec_p (spec);
2054 /* Combine the two exceptions specifier lists LIST and ADD, and return
2055 their union. */
2057 tree
2058 merge_exception_specifiers (tree list, tree add)
2060 tree noex, orig_list;
2062 /* No exception-specifier or noexcept(false) are less strict than
2063 anything else. Prefer the newer variant (LIST). */
2064 if (!list || list == noexcept_false_spec)
2065 return list;
2066 else if (!add || add == noexcept_false_spec)
2067 return add;
2069 /* noexcept(true) and throw() are stricter than anything else.
2070 As above, prefer the more recent one (LIST). */
2071 if (nothrow_spec_p_uninst (add))
2072 return list;
2074 /* Two implicit noexcept specs (e.g. on a destructor) are equivalent. */
2075 if (UNEVALUATED_NOEXCEPT_SPEC_P (add)
2076 && UNEVALUATED_NOEXCEPT_SPEC_P (list))
2077 return list;
2078 /* We should have instantiated other deferred noexcept specs by now. */
2079 gcc_assert (!DEFERRED_NOEXCEPT_SPEC_P (add));
2081 if (nothrow_spec_p_uninst (list))
2082 return add;
2083 noex = TREE_PURPOSE (list);
2084 gcc_checking_assert (!TREE_PURPOSE (add)
2085 || cp_tree_equal (noex, TREE_PURPOSE (add)));
2087 /* Combine the dynamic-exception-specifiers, if any. */
2088 orig_list = list;
2089 for (; add && TREE_VALUE (add); add = TREE_CHAIN (add))
2091 tree spec = TREE_VALUE (add);
2092 tree probe;
2094 for (probe = orig_list; probe && TREE_VALUE (probe);
2095 probe = TREE_CHAIN (probe))
2096 if (same_type_p (TREE_VALUE (probe), spec))
2097 break;
2098 if (!probe)
2100 spec = build_tree_list (NULL_TREE, spec);
2101 TREE_CHAIN (spec) = list;
2102 list = spec;
2106 /* Keep the noexcept-specifier at the beginning of the list. */
2107 if (noex != TREE_PURPOSE (list))
2108 list = tree_cons (noex, TREE_VALUE (list), TREE_CHAIN (list));
2110 return list;
2113 /* Subroutine of build_call. Ensure that each of the types in the
2114 exception specification is complete. Technically, 15.4/1 says that
2115 they need to be complete when we see a declaration of the function,
2116 but we should be able to get away with only requiring this when the
2117 function is defined or called. See also add_exception_specifier. */
2119 void
2120 require_complete_eh_spec_types (tree fntype, tree decl)
2122 tree raises;
2123 /* Don't complain about calls to op new. */
2124 if (decl && DECL_ARTIFICIAL (decl))
2125 return;
2126 for (raises = TYPE_RAISES_EXCEPTIONS (fntype); raises;
2127 raises = TREE_CHAIN (raises))
2129 tree type = TREE_VALUE (raises);
2130 if (type && !COMPLETE_TYPE_P (type))
2132 if (decl)
2133 error
2134 ("call to function %qD which throws incomplete type %q#T",
2135 decl, type);
2136 else
2137 error ("call to function which throws incomplete type %q#T",
2138 decl);
2144 #include "gt-cp-typeck2.h"