2014-07-30 Robert Dewar <dewar@adacore.com>
[official-gcc.git] / gcc / cp / typeck2.c
blob59a47605d6ca04a1dabfaab86d3acb399c5f344b
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"))) 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;
159 /* Compute the hash value of the node VAL. This function is used by the
160 hash table abstract_pending_vars. */
162 static hashval_t
163 pat_calc_hash (const void* val)
165 const struct pending_abstract_type *pat =
166 (const struct pending_abstract_type *) val;
167 return (hashval_t) TYPE_UID (pat->type);
171 /* Compare node VAL1 with the type VAL2. This function is used by the
172 hash table abstract_pending_vars. */
174 static int
175 pat_compare (const void* val1, const void* val2)
177 const struct pending_abstract_type *const pat1 =
178 (const struct pending_abstract_type *) val1;
179 const_tree const type2 = (const_tree)val2;
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 ((param_is (struct pending_abstract_type)))
188 htab_t abstract_pending_vars = NULL;
190 static int abstract_virtuals_error_sfinae (tree, tree, abstract_class_use, tsubst_flags_t);
192 /* This function is called after TYPE is completed, and will check if there
193 are pending declarations for which we still need to verify the abstractness
194 of TYPE, and emit a diagnostic (through abstract_virtuals_error) if TYPE
195 turned out to be incomplete. */
197 void
198 complete_type_check_abstract (tree type)
200 void **slot;
201 struct pending_abstract_type *pat;
202 location_t cur_loc = input_location;
204 gcc_assert (COMPLETE_TYPE_P (type));
206 if (!abstract_pending_vars)
207 return;
209 /* Retrieve the list of pending declarations for this type. */
210 slot = htab_find_slot_with_hash (abstract_pending_vars, type,
211 (hashval_t)TYPE_UID (type), NO_INSERT);
212 if (!slot)
213 return;
214 pat = (struct pending_abstract_type*)*slot;
215 gcc_assert (pat);
217 /* If the type is not abstract, do not do anything. */
218 if (CLASSTYPE_PURE_VIRTUALS (type))
220 struct pending_abstract_type *prev = 0, *next;
222 /* Reverse the list to emit the errors in top-down order. */
223 for (; pat; pat = next)
225 next = pat->next;
226 pat->next = prev;
227 prev = pat;
229 pat = prev;
231 /* Go through the list, and call abstract_virtuals_error for each
232 element: it will issue a diagnostic if the type is abstract. */
233 while (pat)
235 gcc_assert (type == pat->type);
237 /* Tweak input_location so that the diagnostic appears at the correct
238 location. Notice that this is only needed if the decl is an
239 IDENTIFIER_NODE. */
240 input_location = pat->locus;
241 abstract_virtuals_error_sfinae (pat->decl, pat->type, pat->use,
242 tf_warning_or_error);
243 pat = pat->next;
247 htab_clear_slot (abstract_pending_vars, slot);
249 input_location = cur_loc;
253 /* If TYPE has abstract virtual functions, issue an error about trying
254 to create an object of that type. DECL is the object declared, or
255 NULL_TREE if the declaration is unavailable, in which case USE specifies
256 the kind of invalid use. Returns 1 if an error occurred; zero if
257 all was well. */
259 static int
260 abstract_virtuals_error_sfinae (tree decl, tree type, abstract_class_use use,
261 tsubst_flags_t complain)
263 vec<tree, va_gc> *pure;
265 /* This function applies only to classes. Any other entity can never
266 be abstract. */
267 if (!CLASS_TYPE_P (type))
268 return 0;
269 type = TYPE_MAIN_VARIANT (type);
271 #if 0
272 /* Instantiation here seems to be required by the standard,
273 but breaks e.g. boost::bind. FIXME! */
274 /* In SFINAE, non-N3276 context, force instantiation. */
275 if (!(complain & (tf_error|tf_decltype)))
276 complete_type (type);
277 #endif
279 /* If the type is incomplete, we register it within a hash table,
280 so that we can check again once it is completed. This makes sense
281 only for objects for which we have a declaration or at least a
282 name. */
283 if (!COMPLETE_TYPE_P (type) && (complain & tf_error))
285 void **slot;
286 struct pending_abstract_type *pat;
288 gcc_assert (!decl || DECL_P (decl) || identifier_p (decl));
290 if (!abstract_pending_vars)
291 abstract_pending_vars = htab_create_ggc (31, &pat_calc_hash,
292 &pat_compare, NULL);
294 slot = htab_find_slot_with_hash (abstract_pending_vars, type,
295 (hashval_t)TYPE_UID (type), INSERT);
297 pat = ggc_alloc<pending_abstract_type> ();
298 pat->type = type;
299 pat->decl = decl;
300 pat->use = use;
301 pat->locus = ((decl && DECL_P (decl))
302 ? DECL_SOURCE_LOCATION (decl)
303 : input_location);
305 pat->next = (struct pending_abstract_type *) *slot;
306 *slot = pat;
308 return 0;
311 if (!TYPE_SIZE (type))
312 /* TYPE is being defined, and during that time
313 CLASSTYPE_PURE_VIRTUALS holds the inline friends. */
314 return 0;
316 pure = CLASSTYPE_PURE_VIRTUALS (type);
317 if (!pure)
318 return 0;
320 if (!(complain & tf_error))
321 return 1;
323 if (decl)
325 if (VAR_P (decl))
326 error ("cannot declare variable %q+D to be of abstract "
327 "type %qT", decl, type);
328 else if (TREE_CODE (decl) == PARM_DECL)
330 if (DECL_NAME (decl))
331 error ("cannot declare parameter %q+D to be of abstract type %qT",
332 decl, type);
333 else
334 error ("cannot declare parameter to be of abstract type %qT",
335 type);
337 else if (TREE_CODE (decl) == FIELD_DECL)
338 error ("cannot declare field %q+D to be of abstract type %qT",
339 decl, type);
340 else if (TREE_CODE (decl) == FUNCTION_DECL
341 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
342 error ("invalid abstract return type for member function %q+#D", decl);
343 else if (TREE_CODE (decl) == FUNCTION_DECL)
344 error ("invalid abstract return type for function %q+#D", decl);
345 else if (identifier_p (decl))
346 /* Here we do not have location information. */
347 error ("invalid abstract type %qT for %qE", type, decl);
348 else
349 error ("invalid abstract type for %q+D", decl);
351 else switch (use)
353 case ACU_ARRAY:
354 error ("creating array of %qT, which is an abstract class type", type);
355 break;
356 case ACU_CAST:
357 error ("invalid cast to abstract class type %qT", type);
358 break;
359 case ACU_NEW:
360 error ("invalid new-expression of abstract class type %qT", type);
361 break;
362 case ACU_RETURN:
363 error ("invalid abstract return type %qT", type);
364 break;
365 case ACU_PARM:
366 error ("invalid abstract parameter type %qT", type);
367 break;
368 case ACU_THROW:
369 error ("expression of abstract class type %qT cannot "
370 "be used in throw-expression", type);
371 break;
372 case ACU_CATCH:
373 error ("cannot declare catch parameter to be of abstract "
374 "class type %qT", type);
375 break;
376 default:
377 error ("cannot allocate an object of abstract type %qT", type);
380 /* Only go through this once. */
381 if (pure->length ())
383 unsigned ix;
384 tree fn;
386 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
387 " because the following virtual functions are pure within %qT:",
388 type);
390 FOR_EACH_VEC_ELT (*pure, ix, fn)
391 if (! DECL_CLONED_FUNCTION_P (fn)
392 || DECL_COMPLETE_DESTRUCTOR_P (fn))
393 inform (input_location, "\t%+#D", fn);
395 /* Now truncate the vector. This leaves it non-null, so we know
396 there are pure virtuals, but empty so we don't list them out
397 again. */
398 pure->truncate (0);
401 return 1;
405 abstract_virtuals_error_sfinae (tree decl, tree type, tsubst_flags_t complain)
407 return abstract_virtuals_error_sfinae (decl, type, ACU_UNKNOWN, complain);
411 abstract_virtuals_error_sfinae (abstract_class_use use, tree type,
412 tsubst_flags_t complain)
414 return abstract_virtuals_error_sfinae (NULL_TREE, type, use, complain);
418 /* Wrapper for the above function in the common case of wanting errors. */
421 abstract_virtuals_error (tree decl, tree type)
423 return abstract_virtuals_error_sfinae (decl, type, tf_warning_or_error);
427 abstract_virtuals_error (abstract_class_use use, tree type)
429 return abstract_virtuals_error_sfinae (use, type, tf_warning_or_error);
432 /* Print an inform about the declaration of the incomplete type TYPE. */
434 void
435 cxx_incomplete_type_inform (const_tree type)
437 if (!TYPE_MAIN_DECL (type))
438 return;
440 location_t loc = DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type));
441 tree ptype = strip_top_quals (CONST_CAST_TREE (type));
443 if (current_class_type
444 && TYPE_BEING_DEFINED (current_class_type)
445 && same_type_p (ptype, current_class_type))
446 inform (loc, "definition of %q#T is not complete until "
447 "the closing brace", ptype);
448 else if (!TYPE_TEMPLATE_INFO (ptype))
449 inform (loc, "forward declaration of %q#T", ptype);
450 else
451 inform (loc, "declaration of %q#T", ptype);
454 /* Print an error message for invalid use of an incomplete type.
455 VALUE is the expression that was used (or 0 if that isn't known)
456 and TYPE is the type that was invalid. DIAG_KIND indicates the
457 type of diagnostic (see diagnostic.def). */
459 void
460 cxx_incomplete_type_diagnostic (const_tree value, const_tree type,
461 diagnostic_t diag_kind)
463 bool is_decl = false, complained = false;
465 gcc_assert (diag_kind == DK_WARNING
466 || diag_kind == DK_PEDWARN
467 || diag_kind == DK_ERROR);
469 /* Avoid duplicate error message. */
470 if (TREE_CODE (type) == ERROR_MARK)
471 return;
473 if (value != 0 && (VAR_P (value)
474 || TREE_CODE (value) == PARM_DECL
475 || TREE_CODE (value) == FIELD_DECL))
477 complained = emit_diagnostic (diag_kind, input_location, 0,
478 "%q+D has incomplete type", value);
479 is_decl = true;
481 retry:
482 /* We must print an error message. Be clever about what it says. */
484 switch (TREE_CODE (type))
486 case RECORD_TYPE:
487 case UNION_TYPE:
488 case ENUMERAL_TYPE:
489 if (!is_decl)
490 complained = emit_diagnostic (diag_kind, input_location, 0,
491 "invalid use of incomplete type %q#T",
492 type);
493 if (complained)
494 cxx_incomplete_type_inform (type);
495 break;
497 case VOID_TYPE:
498 emit_diagnostic (diag_kind, input_location, 0,
499 "invalid use of %qT", type);
500 break;
502 case ARRAY_TYPE:
503 if (TYPE_DOMAIN (type))
505 type = TREE_TYPE (type);
506 goto retry;
508 emit_diagnostic (diag_kind, input_location, 0,
509 "invalid use of array with unspecified bounds");
510 break;
512 case OFFSET_TYPE:
513 bad_member:
515 tree member = TREE_OPERAND (value, 1);
516 if (is_overloaded_fn (member))
517 member = get_first_fn (member);
518 if (DECL_FUNCTION_MEMBER_P (member)
519 && ! flag_ms_extensions)
520 emit_diagnostic (diag_kind, input_location, 0,
521 "invalid use of member function "
522 "(did you forget the %<()%> ?)");
523 else
524 emit_diagnostic (diag_kind, input_location, 0,
525 "invalid use of member "
526 "(did you forget the %<&%> ?)");
528 break;
530 case TEMPLATE_TYPE_PARM:
531 if (is_auto (type))
532 emit_diagnostic (diag_kind, input_location, 0,
533 "invalid use of %<auto%>");
534 else
535 emit_diagnostic (diag_kind, input_location, 0,
536 "invalid use of template type parameter %qT", type);
537 break;
539 case BOUND_TEMPLATE_TEMPLATE_PARM:
540 emit_diagnostic (diag_kind, input_location, 0,
541 "invalid use of template template parameter %qT",
542 TYPE_NAME (type));
543 break;
545 case TYPENAME_TYPE:
546 emit_diagnostic (diag_kind, input_location, 0,
547 "invalid use of dependent type %qT", type);
548 break;
550 case LANG_TYPE:
551 if (type == init_list_type_node)
553 emit_diagnostic (diag_kind, input_location, 0,
554 "invalid use of brace-enclosed initializer list");
555 break;
557 gcc_assert (type == unknown_type_node);
558 if (value && TREE_CODE (value) == COMPONENT_REF)
559 goto bad_member;
560 else if (value && TREE_CODE (value) == ADDR_EXPR)
561 emit_diagnostic (diag_kind, input_location, 0,
562 "address of overloaded function with no contextual "
563 "type information");
564 else if (value && TREE_CODE (value) == OVERLOAD)
565 emit_diagnostic (diag_kind, input_location, 0,
566 "overloaded function with no contextual type information");
567 else
568 emit_diagnostic (diag_kind, input_location, 0,
569 "insufficient contextual information to determine type");
570 break;
572 default:
573 gcc_unreachable ();
577 /* Backward-compatibility interface to incomplete_type_diagnostic;
578 required by ../tree.c. */
579 #undef cxx_incomplete_type_error
580 void
581 cxx_incomplete_type_error (const_tree value, const_tree type)
583 cxx_incomplete_type_diagnostic (value, type, DK_ERROR);
587 /* The recursive part of split_nonconstant_init. DEST is an lvalue
588 expression to which INIT should be assigned. INIT is a CONSTRUCTOR.
589 Return true if the whole of the value was initialized by the
590 generated statements. */
592 static bool
593 split_nonconstant_init_1 (tree dest, tree init)
595 unsigned HOST_WIDE_INT idx;
596 tree field_index, value;
597 tree type = TREE_TYPE (dest);
598 tree inner_type = NULL;
599 bool array_type_p = false;
600 bool complete_p = true;
601 HOST_WIDE_INT num_split_elts = 0;
603 switch (TREE_CODE (type))
605 case ARRAY_TYPE:
606 inner_type = TREE_TYPE (type);
607 array_type_p = true;
608 /* FALLTHRU */
610 case RECORD_TYPE:
611 case UNION_TYPE:
612 case QUAL_UNION_TYPE:
613 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), idx,
614 field_index, value)
616 /* The current implementation of this algorithm assumes that
617 the field was set for all the elements. This is usually done
618 by process_init_constructor. */
619 gcc_assert (field_index);
621 if (!array_type_p)
622 inner_type = TREE_TYPE (field_index);
624 if (TREE_CODE (value) == CONSTRUCTOR)
626 tree sub;
628 if (array_type_p)
629 sub = build4 (ARRAY_REF, inner_type, dest, field_index,
630 NULL_TREE, NULL_TREE);
631 else
632 sub = build3 (COMPONENT_REF, inner_type, dest, field_index,
633 NULL_TREE);
635 if (!split_nonconstant_init_1 (sub, value))
636 complete_p = false;
637 num_split_elts++;
639 else if (!initializer_constant_valid_p (value, inner_type))
641 tree code;
642 tree sub;
644 /* FIXME: Ordered removal is O(1) so the whole function is
645 worst-case quadratic. This could be fixed using an aside
646 bitmap to record which elements must be removed and remove
647 them all at the same time. Or by merging
648 split_non_constant_init into process_init_constructor_array,
649 that is separating constants from non-constants while building
650 the vector. */
651 CONSTRUCTOR_ELTS (init)->ordered_remove (idx);
652 --idx;
654 if (TREE_CODE (field_index) == RANGE_EXPR)
656 /* Use build_vec_init to initialize a range. */
657 tree low = TREE_OPERAND (field_index, 0);
658 tree hi = TREE_OPERAND (field_index, 1);
659 sub = build4 (ARRAY_REF, inner_type, dest, low,
660 NULL_TREE, NULL_TREE);
661 sub = cp_build_addr_expr (sub, tf_warning_or_error);
662 tree max = size_binop (MINUS_EXPR, hi, low);
663 code = build_vec_init (sub, max, value, false, 0,
664 tf_warning_or_error);
665 add_stmt (code);
666 if (tree_fits_shwi_p (max))
667 num_split_elts += tree_to_shwi (max);
669 else
671 if (array_type_p)
672 sub = build4 (ARRAY_REF, inner_type, dest, field_index,
673 NULL_TREE, NULL_TREE);
674 else
675 sub = build3 (COMPONENT_REF, inner_type, dest, field_index,
676 NULL_TREE);
678 code = build2 (INIT_EXPR, inner_type, sub, value);
679 code = build_stmt (input_location, EXPR_STMT, code);
680 code = maybe_cleanup_point_expr_void (code);
681 add_stmt (code);
682 if (type_build_dtor_call (inner_type))
684 code = (build_special_member_call
685 (sub, complete_dtor_identifier, NULL, inner_type,
686 LOOKUP_NORMAL, tf_warning_or_error));
687 if (!TYPE_HAS_TRIVIAL_DESTRUCTOR (inner_type))
688 finish_eh_cleanup (code);
692 num_split_elts++;
695 break;
697 case VECTOR_TYPE:
698 if (!initializer_constant_valid_p (init, type))
700 tree code;
701 tree cons = copy_node (init);
702 CONSTRUCTOR_ELTS (init) = NULL;
703 code = build2 (MODIFY_EXPR, type, dest, cons);
704 code = build_stmt (input_location, EXPR_STMT, code);
705 add_stmt (code);
706 num_split_elts += CONSTRUCTOR_NELTS (init);
708 break;
710 default:
711 gcc_unreachable ();
714 /* The rest of the initializer is now a constant. */
715 TREE_CONSTANT (init) = 1;
716 return complete_p && complete_ctor_at_level_p (TREE_TYPE (init),
717 num_split_elts, inner_type);
720 /* A subroutine of store_init_value. Splits non-constant static
721 initializer INIT into a constant part and generates code to
722 perform the non-constant part of the initialization to DEST.
723 Returns the code for the runtime init. */
725 static tree
726 split_nonconstant_init (tree dest, tree init)
728 tree code;
730 if (TREE_CODE (init) == CONSTRUCTOR)
732 code = push_stmt_list ();
733 if (split_nonconstant_init_1 (dest, init))
734 init = NULL_TREE;
735 code = pop_stmt_list (code);
736 DECL_INITIAL (dest) = init;
737 TREE_READONLY (dest) = 0;
739 else
740 code = build2 (INIT_EXPR, TREE_TYPE (dest), dest, init);
742 return code;
745 /* Perform appropriate conversions on the initial value of a variable,
746 store it in the declaration DECL,
747 and print any error messages that are appropriate.
748 If the init is invalid, store an ERROR_MARK.
750 C++: Note that INIT might be a TREE_LIST, which would mean that it is
751 a base class initializer for some aggregate type, hopefully compatible
752 with DECL. If INIT is a single element, and DECL is an aggregate
753 type, we silently convert INIT into a TREE_LIST, allowing a constructor
754 to be called.
756 If INIT is a TREE_LIST and there is no constructor, turn INIT
757 into a CONSTRUCTOR and use standard initialization techniques.
758 Perhaps a warning should be generated?
760 Returns code to be executed if initialization could not be performed
761 for static variable. In that case, caller must emit the code. */
763 tree
764 store_init_value (tree decl, tree init, vec<tree, va_gc>** cleanups, int flags)
766 tree value, type;
768 /* If variable's type was invalidly declared, just ignore it. */
770 type = TREE_TYPE (decl);
771 if (TREE_CODE (type) == ERROR_MARK)
772 return NULL_TREE;
774 if (MAYBE_CLASS_TYPE_P (type))
776 if (TREE_CODE (init) == TREE_LIST)
778 error ("constructor syntax used, but no constructor declared "
779 "for type %qT", type);
780 init = build_constructor_from_list (init_list_type_node, nreverse (init));
784 /* End of special C++ code. */
786 if (flags & LOOKUP_ALREADY_DIGESTED)
787 value = init;
788 else
789 /* Digest the specified initializer into an expression. */
790 value = digest_init_flags (type, init, flags);
792 value = extend_ref_init_temps (decl, value, cleanups);
794 /* In C++0x constant expression is a semantic, not syntactic, property.
795 In C++98, make sure that what we thought was a constant expression at
796 template definition time is still constant and otherwise perform this
797 as optimization, e.g. to fold SIZEOF_EXPRs in the initializer. */
798 if (decl_maybe_constant_var_p (decl) || TREE_STATIC (decl))
800 bool const_init;
801 value = fold_non_dependent_expr (value);
802 if (DECL_DECLARED_CONSTEXPR_P (decl)
803 || DECL_IN_AGGR_P (decl))
805 /* Diagnose a non-constant initializer for constexpr. */
806 if (processing_template_decl
807 && !require_potential_constant_expression (value))
808 value = error_mark_node;
809 else
810 value = cxx_constant_value (value);
812 value = maybe_constant_init (value);
813 const_init = (reduced_constant_expression_p (value)
814 || error_operand_p (value));
815 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = const_init;
816 TREE_CONSTANT (decl) = const_init && decl_maybe_constant_var_p (decl);
819 /* If the initializer is not a constant, fill in DECL_INITIAL with
820 the bits that are constant, and then return an expression that
821 will perform the dynamic initialization. */
822 if (value != error_mark_node
823 && (TREE_SIDE_EFFECTS (value)
824 || array_of_runtime_bound_p (type)
825 || ! reduced_constant_expression_p (value)))
827 if (TREE_CODE (type) == ARRAY_TYPE
828 && (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (type))
829 || array_of_runtime_bound_p (type)))
830 /* For an array, we only need/want a single cleanup region rather
831 than one per element. */
832 return build_vec_init (decl, NULL_TREE, value, false, 1,
833 tf_warning_or_error);
834 else
835 return split_nonconstant_init (decl, value);
837 /* If the value is a constant, just put it in DECL_INITIAL. If DECL
838 is an automatic variable, the middle end will turn this into a
839 dynamic initialization later. */
840 DECL_INITIAL (decl) = value;
841 return NULL_TREE;
845 /* Give errors about narrowing conversions within { }. */
847 void
848 check_narrowing (tree type, tree init)
850 tree ftype = unlowered_expr_type (init);
851 bool ok = true;
852 REAL_VALUE_TYPE d;
854 if (!warn_narrowing || !ARITHMETIC_TYPE_P (type))
855 return;
857 if (BRACE_ENCLOSED_INITIALIZER_P (init)
858 && TREE_CODE (type) == COMPLEX_TYPE)
860 tree elttype = TREE_TYPE (type);
861 if (CONSTRUCTOR_NELTS (init) > 0)
862 check_narrowing (elttype, CONSTRUCTOR_ELT (init, 0)->value);
863 if (CONSTRUCTOR_NELTS (init) > 1)
864 check_narrowing (elttype, CONSTRUCTOR_ELT (init, 1)->value);
865 return;
868 init = maybe_constant_value (fold_non_dependent_expr_sfinae (init, tf_none));
870 if (TREE_CODE (type) == INTEGER_TYPE
871 && TREE_CODE (ftype) == REAL_TYPE)
872 ok = false;
873 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype)
874 && CP_INTEGRAL_TYPE_P (type))
876 if (TREE_CODE (ftype) == ENUMERAL_TYPE)
877 /* Check for narrowing based on the values of the enumeration. */
878 ftype = ENUM_UNDERLYING_TYPE (ftype);
879 if ((tree_int_cst_lt (TYPE_MAX_VALUE (type),
880 TYPE_MAX_VALUE (ftype))
881 || tree_int_cst_lt (TYPE_MIN_VALUE (ftype),
882 TYPE_MIN_VALUE (type)))
883 && (TREE_CODE (init) != INTEGER_CST
884 || !int_fits_type_p (init, type)))
885 ok = false;
887 else if (TREE_CODE (ftype) == REAL_TYPE
888 && TREE_CODE (type) == REAL_TYPE)
890 if (TYPE_PRECISION (type) < TYPE_PRECISION (ftype))
892 if (TREE_CODE (init) == REAL_CST)
894 /* Issue 703: Loss of precision is OK as long as the value is
895 within the representable range of the new type. */
896 REAL_VALUE_TYPE r;
897 d = TREE_REAL_CST (init);
898 real_convert (&r, TYPE_MODE (type), &d);
899 if (real_isinf (&r))
900 ok = false;
902 else
903 ok = false;
906 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype)
907 && TREE_CODE (type) == REAL_TYPE)
909 ok = false;
910 if (TREE_CODE (init) == INTEGER_CST)
912 d = real_value_from_int_cst (0, init);
913 if (exact_real_truncate (TYPE_MODE (type), &d))
914 ok = true;
918 if (!ok)
920 if (cxx_dialect >= cxx11)
921 pedwarn (EXPR_LOC_OR_LOC (init, input_location), OPT_Wnarrowing,
922 "narrowing conversion of %qE from %qT to %qT inside { }",
923 init, ftype, type);
924 else
925 warning_at (EXPR_LOC_OR_LOC (init, input_location), OPT_Wnarrowing,
926 "narrowing conversion of %qE from %qT to %qT inside { } "
927 "is ill-formed in C++11", init, ftype, type);
931 /* Process the initializer INIT for a variable of type TYPE, emitting
932 diagnostics for invalid initializers and converting the initializer as
933 appropriate.
935 For aggregate types, it assumes that reshape_init has already run, thus the
936 initializer will have the right shape (brace elision has been undone).
938 NESTED is true iff we are being called for an element of a CONSTRUCTOR. */
940 static tree
941 digest_init_r (tree type, tree init, bool nested, int flags,
942 tsubst_flags_t complain)
944 enum tree_code code = TREE_CODE (type);
946 if (error_operand_p (init))
947 return error_mark_node;
949 gcc_assert (init);
951 /* We must strip the outermost array type when completing the type,
952 because the its bounds might be incomplete at the moment. */
953 if (!complete_type_or_maybe_complain (TREE_CODE (type) == ARRAY_TYPE
954 ? TREE_TYPE (type) : type, NULL_TREE,
955 complain))
956 return error_mark_node;
958 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue
959 (g++.old-deja/g++.law/casts2.C). */
960 if (TREE_CODE (init) == NON_LVALUE_EXPR)
961 init = TREE_OPERAND (init, 0);
963 /* Initialization of an array of chars from a string constant. The initializer
964 can be optionally enclosed in braces, but reshape_init has already removed
965 them if they were present. */
966 if (code == ARRAY_TYPE)
968 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
969 if (char_type_p (typ1)
970 /*&& init */
971 && TREE_CODE (init) == STRING_CST)
973 tree char_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (init)));
975 if (TYPE_PRECISION (typ1) == BITS_PER_UNIT)
977 if (char_type != char_type_node)
979 if (complain & tf_error)
980 error ("char-array initialized from wide string");
981 return error_mark_node;
984 else
986 if (char_type == char_type_node)
988 if (complain & tf_error)
989 error ("int-array initialized from non-wide string");
990 return error_mark_node;
992 else if (char_type != typ1)
994 if (complain & tf_error)
995 error ("int-array initialized from incompatible "
996 "wide string");
997 return error_mark_node;
1001 if (type != TREE_TYPE (init))
1003 init = copy_node (init);
1004 TREE_TYPE (init) = type;
1006 if (TYPE_DOMAIN (type) != 0 && TREE_CONSTANT (TYPE_SIZE (type)))
1008 int size = TREE_INT_CST_LOW (TYPE_SIZE (type));
1009 size = (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
1010 /* In C it is ok to subtract 1 from the length of the string
1011 because it's ok to ignore the terminating null char that is
1012 counted in the length of the constant, but in C++ this would
1013 be invalid. */
1014 if (size < TREE_STRING_LENGTH (init))
1015 permerror (input_location, "initializer-string for array "
1016 "of chars is too long");
1018 return init;
1022 /* Handle scalar types (including conversions) and references. */
1023 if ((TREE_CODE (type) != COMPLEX_TYPE
1024 || BRACE_ENCLOSED_INITIALIZER_P (init))
1025 && (SCALAR_TYPE_P (type) || code == REFERENCE_TYPE))
1027 tree *exp;
1029 if (nested)
1030 flags |= LOOKUP_NO_NARROWING;
1031 init = convert_for_initialization (0, type, init, flags,
1032 ICR_INIT, NULL_TREE, 0,
1033 complain);
1034 exp = &init;
1036 /* Skip any conversions since we'll be outputting the underlying
1037 constant. */
1038 while (CONVERT_EXPR_P (*exp)
1039 || TREE_CODE (*exp) == NON_LVALUE_EXPR)
1040 exp = &TREE_OPERAND (*exp, 0);
1042 *exp = cplus_expand_constant (*exp);
1044 return init;
1047 /* Come here only for aggregates: records, arrays, unions, complex numbers
1048 and vectors. */
1049 gcc_assert (TREE_CODE (type) == ARRAY_TYPE
1050 || TREE_CODE (type) == VECTOR_TYPE
1051 || TREE_CODE (type) == RECORD_TYPE
1052 || TREE_CODE (type) == UNION_TYPE
1053 || TREE_CODE (type) == COMPLEX_TYPE);
1055 if (BRACE_ENCLOSED_INITIALIZER_P (init)
1056 && !TYPE_NON_AGGREGATE_CLASS (type))
1057 return process_init_constructor (type, init, complain);
1058 else
1060 if (COMPOUND_LITERAL_P (init) && TREE_CODE (type) == ARRAY_TYPE)
1062 if (complain & tf_error)
1063 error ("cannot initialize aggregate of type %qT with "
1064 "a compound literal", type);
1066 return error_mark_node;
1069 if (TREE_CODE (type) == ARRAY_TYPE
1070 && !BRACE_ENCLOSED_INITIALIZER_P (init))
1072 /* Allow the result of build_array_copy and of
1073 build_value_init_noctor. */
1074 if ((TREE_CODE (init) == VEC_INIT_EXPR
1075 || TREE_CODE (init) == CONSTRUCTOR)
1076 && (same_type_ignoring_top_level_qualifiers_p
1077 (type, TREE_TYPE (init))))
1078 return init;
1080 if (complain & tf_error)
1081 error ("array must be initialized with a brace-enclosed"
1082 " initializer");
1083 return error_mark_node;
1086 return convert_for_initialization (NULL_TREE, type, init,
1087 flags,
1088 ICR_INIT, NULL_TREE, 0,
1089 complain);
1093 tree
1094 digest_init (tree type, tree init, tsubst_flags_t complain)
1096 return digest_init_r (type, init, false, LOOKUP_IMPLICIT, complain);
1099 tree
1100 digest_init_flags (tree type, tree init, int flags)
1102 return digest_init_r (type, init, false, flags, tf_warning_or_error);
1105 /* Process the initializer INIT for an NSDMI DECL (a FIELD_DECL). */
1106 tree
1107 digest_nsdmi_init (tree decl, tree init)
1109 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
1111 int flags = LOOKUP_IMPLICIT;
1112 if (DIRECT_LIST_INIT_P (init))
1113 flags = LOOKUP_NORMAL;
1114 init = digest_init_flags (TREE_TYPE (decl), init, flags);
1115 if (TREE_CODE (init) == TARGET_EXPR)
1116 /* This represents the whole initialization. */
1117 TARGET_EXPR_DIRECT_INIT_P (init) = true;
1118 return init;
1121 /* Set of flags used within process_init_constructor to describe the
1122 initializers. */
1123 #define PICFLAG_ERRONEOUS 1
1124 #define PICFLAG_NOT_ALL_CONSTANT 2
1125 #define PICFLAG_NOT_ALL_SIMPLE 4
1126 #define PICFLAG_SIDE_EFFECTS 8
1128 /* Given an initializer INIT, return the flag (PICFLAG_*) which better
1129 describe it. */
1131 static int
1132 picflag_from_initializer (tree init)
1134 if (init == error_mark_node)
1135 return PICFLAG_ERRONEOUS;
1136 else if (!TREE_CONSTANT (init))
1138 if (TREE_SIDE_EFFECTS (init))
1139 return PICFLAG_SIDE_EFFECTS;
1140 else
1141 return PICFLAG_NOT_ALL_CONSTANT;
1143 else if (!initializer_constant_valid_p (init, TREE_TYPE (init)))
1144 return PICFLAG_NOT_ALL_SIMPLE;
1145 return 0;
1148 /* Adjust INIT for going into a CONSTRUCTOR. */
1150 static tree
1151 massage_init_elt (tree type, tree init, tsubst_flags_t complain)
1153 init = digest_init_r (type, init, true, LOOKUP_IMPLICIT, complain);
1154 /* Strip a simple TARGET_EXPR when we know this is an initializer. */
1155 if (TREE_CODE (init) == TARGET_EXPR
1156 && !VOID_TYPE_P (TREE_TYPE (TARGET_EXPR_INITIAL (init))))
1157 init = TARGET_EXPR_INITIAL (init);
1158 /* When we defer constant folding within a statement, we may want to
1159 defer this folding as well. */
1160 tree t = fold_non_dependent_expr_sfinae (init, complain);
1161 t = maybe_constant_init (t);
1162 if (TREE_CONSTANT (t))
1163 init = t;
1164 return init;
1167 /* Subroutine of process_init_constructor, which will process an initializer
1168 INIT for an array or vector of type TYPE. Returns the flags (PICFLAG_*)
1169 which describe the initializers. */
1171 static int
1172 process_init_constructor_array (tree type, tree init,
1173 tsubst_flags_t complain)
1175 unsigned HOST_WIDE_INT i, len = 0;
1176 int flags = 0;
1177 bool unbounded = false;
1178 constructor_elt *ce;
1179 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (init);
1181 gcc_assert (TREE_CODE (type) == ARRAY_TYPE
1182 || TREE_CODE (type) == VECTOR_TYPE);
1184 if (TREE_CODE (type) == ARRAY_TYPE)
1186 tree domain = TYPE_DOMAIN (type);
1187 if (domain && TREE_CONSTANT (TYPE_MAX_VALUE (domain)))
1188 len = wi::ext (wi::to_offset (TYPE_MAX_VALUE (domain))
1189 - wi::to_offset (TYPE_MIN_VALUE (domain)) + 1,
1190 TYPE_PRECISION (TREE_TYPE (domain)),
1191 TYPE_SIGN (TREE_TYPE (domain))).to_uhwi ();
1192 else
1193 unbounded = true; /* Take as many as there are. */
1195 else
1196 /* Vectors are like simple fixed-size arrays. */
1197 len = TYPE_VECTOR_SUBPARTS (type);
1199 /* There must not be more initializers than needed. */
1200 if (!unbounded && vec_safe_length (v) > len)
1202 if (complain & tf_error)
1203 error ("too many initializers for %qT", type);
1204 else
1205 return PICFLAG_ERRONEOUS;
1208 FOR_EACH_VEC_SAFE_ELT (v, i, ce)
1210 if (ce->index)
1212 gcc_assert (TREE_CODE (ce->index) == INTEGER_CST);
1213 if (compare_tree_int (ce->index, i) != 0)
1215 ce->value = error_mark_node;
1216 sorry ("non-trivial designated initializers not supported");
1219 else
1220 ce->index = size_int (i);
1221 gcc_assert (ce->value);
1222 ce->value = massage_init_elt (TREE_TYPE (type), ce->value, complain);
1224 if (ce->value != error_mark_node)
1225 gcc_assert (same_type_ignoring_top_level_qualifiers_p
1226 (TREE_TYPE (type), TREE_TYPE (ce->value)));
1228 flags |= picflag_from_initializer (ce->value);
1231 /* No more initializers. If the array is unbounded, we are done. Otherwise,
1232 we must add initializers ourselves. */
1233 if (!unbounded)
1234 for (; i < len; ++i)
1236 tree next;
1238 if (type_build_ctor_call (TREE_TYPE (type)))
1240 /* If this type needs constructors run for default-initialization,
1241 we can't rely on the back end to do it for us, so make the
1242 initialization explicit by list-initializing from {}. */
1243 next = build_constructor (init_list_type_node, NULL);
1244 next = massage_init_elt (TREE_TYPE (type), next, complain);
1245 if (initializer_zerop (next))
1246 /* The default zero-initialization is fine for us; don't
1247 add anything to the CONSTRUCTOR. */
1248 next = NULL_TREE;
1250 else if (!zero_init_p (TREE_TYPE (type)))
1251 next = build_zero_init (TREE_TYPE (type),
1252 /*nelts=*/NULL_TREE,
1253 /*static_storage_p=*/false);
1254 else
1255 /* The default zero-initialization is fine for us; don't
1256 add anything to the CONSTRUCTOR. */
1257 next = NULL_TREE;
1259 if (next)
1261 flags |= picflag_from_initializer (next);
1262 CONSTRUCTOR_APPEND_ELT (v, size_int (i), next);
1266 CONSTRUCTOR_ELTS (init) = v;
1267 return flags;
1270 /* Subroutine of process_init_constructor, which will process an initializer
1271 INIT for a class of type TYPE. Returns the flags (PICFLAG_*) which describe
1272 the initializers. */
1274 static int
1275 process_init_constructor_record (tree type, tree init,
1276 tsubst_flags_t complain)
1278 vec<constructor_elt, va_gc> *v = NULL;
1279 int flags = 0;
1280 tree field;
1281 unsigned HOST_WIDE_INT idx = 0;
1283 gcc_assert (TREE_CODE (type) == RECORD_TYPE);
1284 gcc_assert (!CLASSTYPE_VBASECLASSES (type));
1285 gcc_assert (!TYPE_BINFO (type)
1286 || !BINFO_N_BASE_BINFOS (TYPE_BINFO (type)));
1287 gcc_assert (!TYPE_POLYMORPHIC_P (type));
1289 /* Generally, we will always have an index for each initializer (which is
1290 a FIELD_DECL, put by reshape_init), but compound literals don't go trough
1291 reshape_init. So we need to handle both cases. */
1292 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
1294 tree next;
1295 tree type;
1297 if (!DECL_NAME (field) && DECL_C_BIT_FIELD (field))
1298 continue;
1300 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
1301 continue;
1303 /* If this is a bitfield, first convert to the declared type. */
1304 type = TREE_TYPE (field);
1305 if (DECL_BIT_FIELD_TYPE (field))
1306 type = DECL_BIT_FIELD_TYPE (field);
1307 if (type == error_mark_node)
1308 return PICFLAG_ERRONEOUS;
1310 if (idx < vec_safe_length (CONSTRUCTOR_ELTS (init)))
1312 constructor_elt *ce = &(*CONSTRUCTOR_ELTS (init))[idx];
1313 if (ce->index)
1315 /* We can have either a FIELD_DECL or an IDENTIFIER_NODE. The
1316 latter case can happen in templates where lookup has to be
1317 deferred. */
1318 gcc_assert (TREE_CODE (ce->index) == FIELD_DECL
1319 || identifier_p (ce->index));
1320 if (ce->index != field
1321 && ce->index != DECL_NAME (field))
1323 ce->value = error_mark_node;
1324 sorry ("non-trivial designated initializers not supported");
1328 gcc_assert (ce->value);
1329 next = massage_init_elt (type, ce->value, complain);
1330 ++idx;
1332 else if (type_build_ctor_call (TREE_TYPE (field)))
1334 /* If this type needs constructors run for
1335 default-initialization, we can't rely on the back end to do it
1336 for us, so build up TARGET_EXPRs. If the type in question is
1337 a class, just build one up; if it's an array, recurse. */
1338 next = build_constructor (init_list_type_node, NULL);
1339 /* Call this direct-initialization pending DR 1518 resolution so
1340 that explicit default ctors don't break valid C++03 code. */
1341 CONSTRUCTOR_IS_DIRECT_INIT (next) = true;
1342 next = massage_init_elt (TREE_TYPE (field), next, complain);
1344 /* Warn when some struct elements are implicitly initialized. */
1345 if (complain & tf_warning)
1346 warning (OPT_Wmissing_field_initializers,
1347 "missing initializer for member %qD", field);
1349 else
1351 if (TREE_CODE (TREE_TYPE (field)) == REFERENCE_TYPE)
1353 if (complain & tf_error)
1354 error ("member %qD is uninitialized reference", field);
1355 else
1356 return PICFLAG_ERRONEOUS;
1358 else if (CLASSTYPE_REF_FIELDS_NEED_INIT (TREE_TYPE (field)))
1360 if (complain & tf_error)
1361 error ("member %qD with uninitialized reference fields", field);
1362 else
1363 return PICFLAG_ERRONEOUS;
1366 /* Warn when some struct elements are implicitly initialized
1367 to zero. */
1368 if (complain & tf_warning)
1369 warning (OPT_Wmissing_field_initializers,
1370 "missing initializer for member %qD", field);
1372 if (!zero_init_p (TREE_TYPE (field)))
1373 next = build_zero_init (TREE_TYPE (field), /*nelts=*/NULL_TREE,
1374 /*static_storage_p=*/false);
1375 else
1376 /* The default zero-initialization is fine for us; don't
1377 add anything to the CONSTRUCTOR. */
1378 continue;
1381 /* If this is a bitfield, now convert to the lowered type. */
1382 if (type != TREE_TYPE (field))
1383 next = cp_convert_and_check (TREE_TYPE (field), next, complain);
1384 flags |= picflag_from_initializer (next);
1385 CONSTRUCTOR_APPEND_ELT (v, field, next);
1388 if (idx < vec_safe_length (CONSTRUCTOR_ELTS (init)))
1390 if (complain & tf_error)
1391 error ("too many initializers for %qT", type);
1392 else
1393 return PICFLAG_ERRONEOUS;
1396 CONSTRUCTOR_ELTS (init) = v;
1397 return flags;
1400 /* Subroutine of process_init_constructor, which will process a single
1401 initializer INIT for a union of type TYPE. Returns the flags (PICFLAG_*)
1402 which describe the initializer. */
1404 static int
1405 process_init_constructor_union (tree type, tree init,
1406 tsubst_flags_t complain)
1408 constructor_elt *ce;
1409 int len;
1411 /* If the initializer was empty, use default zero initialization. */
1412 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
1413 return 0;
1415 len = CONSTRUCTOR_ELTS (init)->length ();
1416 if (len > 1)
1418 if (!(complain & tf_error))
1419 return PICFLAG_ERRONEOUS;
1420 error ("too many initializers for %qT", type);
1421 CONSTRUCTOR_ELTS (init)->block_remove (1, len-1);
1424 ce = &(*CONSTRUCTOR_ELTS (init))[0];
1426 /* If this element specifies a field, initialize via that field. */
1427 if (ce->index)
1429 if (TREE_CODE (ce->index) == FIELD_DECL)
1431 else if (identifier_p (ce->index))
1433 /* This can happen within a cast, see g++.dg/opt/cse2.C. */
1434 tree name = ce->index;
1435 tree field;
1436 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1437 if (DECL_NAME (field) == name)
1438 break;
1439 if (!field)
1441 if (complain & tf_error)
1442 error ("no field %qD found in union being initialized",
1443 field);
1444 ce->value = error_mark_node;
1446 ce->index = field;
1448 else
1450 gcc_assert (TREE_CODE (ce->index) == INTEGER_CST
1451 || TREE_CODE (ce->index) == RANGE_EXPR);
1452 if (complain & tf_error)
1453 error ("index value instead of field name in union initializer");
1454 ce->value = error_mark_node;
1457 else
1459 /* Find the first named field. ANSI decided in September 1990
1460 that only named fields count here. */
1461 tree field = TYPE_FIELDS (type);
1462 while (field && (!DECL_NAME (field) || TREE_CODE (field) != FIELD_DECL))
1463 field = TREE_CHAIN (field);
1464 if (field == NULL_TREE)
1466 if (complain & tf_error)
1467 error ("too many initializers for %qT", type);
1468 ce->value = error_mark_node;
1470 ce->index = field;
1473 if (ce->value && ce->value != error_mark_node)
1474 ce->value = massage_init_elt (TREE_TYPE (ce->index), ce->value, complain);
1476 return picflag_from_initializer (ce->value);
1479 /* Process INIT, a constructor for a variable of aggregate type TYPE. The
1480 constructor is a brace-enclosed initializer, and will be modified in-place.
1482 Each element is converted to the right type through digest_init, and
1483 missing initializers are added following the language rules (zero-padding,
1484 etc.).
1486 After the execution, the initializer will have TREE_CONSTANT if all elts are
1487 constant, and TREE_STATIC set if, in addition, all elts are simple enough
1488 constants that the assembler and linker can compute them.
1490 The function returns the initializer itself, or error_mark_node in case
1491 of error. */
1493 static tree
1494 process_init_constructor (tree type, tree init, tsubst_flags_t complain)
1496 int flags;
1498 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
1500 if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == VECTOR_TYPE)
1501 flags = process_init_constructor_array (type, init, complain);
1502 else if (TREE_CODE (type) == RECORD_TYPE)
1503 flags = process_init_constructor_record (type, init, complain);
1504 else if (TREE_CODE (type) == UNION_TYPE)
1505 flags = process_init_constructor_union (type, init, complain);
1506 else
1507 gcc_unreachable ();
1509 if (flags & PICFLAG_ERRONEOUS)
1510 return error_mark_node;
1512 TREE_TYPE (init) = type;
1513 if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == NULL_TREE)
1514 cp_complete_array_type (&TREE_TYPE (init), init, /*do_default=*/0);
1515 if (flags & PICFLAG_SIDE_EFFECTS)
1517 TREE_CONSTANT (init) = false;
1518 TREE_SIDE_EFFECTS (init) = true;
1520 else if (flags & PICFLAG_NOT_ALL_CONSTANT)
1521 /* Make sure TREE_CONSTANT isn't set from build_constructor. */
1522 TREE_CONSTANT (init) = false;
1523 else
1525 TREE_CONSTANT (init) = 1;
1526 if (!(flags & PICFLAG_NOT_ALL_SIMPLE))
1527 TREE_STATIC (init) = 1;
1529 return init;
1532 /* Given a structure or union value DATUM, construct and return
1533 the structure or union component which results from narrowing
1534 that value to the base specified in BASETYPE. For example, given the
1535 hierarchy
1537 class L { int ii; };
1538 class A : L { ... };
1539 class B : L { ... };
1540 class C : A, B { ... };
1542 and the declaration
1544 C x;
1546 then the expression
1548 x.A::ii refers to the ii member of the L part of
1549 the A part of the C object named by X. In this case,
1550 DATUM would be x, and BASETYPE would be A.
1552 I used to think that this was nonconformant, that the standard specified
1553 that first we look up ii in A, then convert x to an L& and pull out the
1554 ii part. But in fact, it does say that we convert x to an A&; A here
1555 is known as the "naming class". (jason 2000-12-19)
1557 BINFO_P points to a variable initialized either to NULL_TREE or to the
1558 binfo for the specific base subobject we want to convert to. */
1560 tree
1561 build_scoped_ref (tree datum, tree basetype, tree* binfo_p)
1563 tree binfo;
1565 if (datum == error_mark_node)
1566 return error_mark_node;
1567 if (*binfo_p)
1568 binfo = *binfo_p;
1569 else
1570 binfo = lookup_base (TREE_TYPE (datum), basetype, ba_check,
1571 NULL, tf_warning_or_error);
1573 if (!binfo || binfo == error_mark_node)
1575 *binfo_p = NULL_TREE;
1576 if (!binfo)
1577 error_not_base_type (basetype, TREE_TYPE (datum));
1578 return error_mark_node;
1581 *binfo_p = binfo;
1582 return build_base_path (PLUS_EXPR, datum, binfo, 1,
1583 tf_warning_or_error);
1586 /* Build a reference to an object specified by the C++ `->' operator.
1587 Usually this just involves dereferencing the object, but if the
1588 `->' operator is overloaded, then such overloads must be
1589 performed until an object which does not have the `->' operator
1590 overloaded is found. An error is reported when circular pointer
1591 delegation is detected. */
1593 tree
1594 build_x_arrow (location_t loc, tree expr, tsubst_flags_t complain)
1596 tree orig_expr = expr;
1597 tree type = TREE_TYPE (expr);
1598 tree last_rval = NULL_TREE;
1599 vec<tree, va_gc> *types_memoized = NULL;
1601 if (type == error_mark_node)
1602 return error_mark_node;
1604 if (processing_template_decl)
1606 if (type_dependent_expression_p (expr))
1607 return build_min_nt_loc (loc, ARROW_EXPR, expr);
1608 expr = build_non_dependent_expr (expr);
1611 if (MAYBE_CLASS_TYPE_P (type))
1613 struct tinst_level *actual_inst = current_instantiation ();
1614 tree fn = NULL;
1616 while ((expr = build_new_op (loc, COMPONENT_REF,
1617 LOOKUP_NORMAL, expr, NULL_TREE, NULL_TREE,
1618 &fn, complain)))
1620 if (expr == error_mark_node)
1621 return error_mark_node;
1623 if (fn && DECL_USE_TEMPLATE (fn))
1624 push_tinst_level (fn);
1625 fn = NULL;
1627 if (vec_member (TREE_TYPE (expr), types_memoized))
1629 if (complain & tf_error)
1630 error ("circular pointer delegation detected");
1631 return error_mark_node;
1634 vec_safe_push (types_memoized, TREE_TYPE (expr));
1635 last_rval = expr;
1638 while (current_instantiation () != actual_inst)
1639 pop_tinst_level ();
1641 if (last_rval == NULL_TREE)
1643 if (complain & tf_error)
1644 error ("base operand of %<->%> has non-pointer type %qT", type);
1645 return error_mark_node;
1648 if (TREE_CODE (TREE_TYPE (last_rval)) == REFERENCE_TYPE)
1649 last_rval = convert_from_reference (last_rval);
1651 else
1652 last_rval = decay_conversion (expr, complain);
1654 if (TYPE_PTR_P (TREE_TYPE (last_rval)))
1656 if (processing_template_decl)
1658 expr = build_min (ARROW_EXPR, TREE_TYPE (TREE_TYPE (last_rval)),
1659 orig_expr);
1660 TREE_SIDE_EFFECTS (expr) = TREE_SIDE_EFFECTS (last_rval);
1661 return expr;
1664 return cp_build_indirect_ref (last_rval, RO_NULL, complain);
1667 if (complain & tf_error)
1669 if (types_memoized)
1670 error ("result of %<operator->()%> yields non-pointer result");
1671 else
1672 error ("base operand of %<->%> is not a pointer");
1674 return error_mark_node;
1677 /* Return an expression for "DATUM .* COMPONENT". DATUM has not
1678 already been checked out to be of aggregate type. */
1680 tree
1681 build_m_component_ref (tree datum, tree component, tsubst_flags_t complain)
1683 tree ptrmem_type;
1684 tree objtype;
1685 tree type;
1686 tree binfo;
1687 tree ctype;
1689 if (error_operand_p (datum) || error_operand_p (component))
1690 return error_mark_node;
1692 datum = mark_lvalue_use (datum);
1693 component = mark_rvalue_use (component);
1695 ptrmem_type = TREE_TYPE (component);
1696 if (!TYPE_PTRMEM_P (ptrmem_type))
1698 if (complain & tf_error)
1699 error ("%qE cannot be used as a member pointer, since it is of "
1700 "type %qT", component, ptrmem_type);
1701 return error_mark_node;
1704 objtype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
1705 if (! MAYBE_CLASS_TYPE_P (objtype))
1707 if (complain & tf_error)
1708 error ("cannot apply member pointer %qE to %qE, which is of "
1709 "non-class type %qT", component, datum, objtype);
1710 return error_mark_node;
1713 type = TYPE_PTRMEM_POINTED_TO_TYPE (ptrmem_type);
1714 ctype = complete_type (TYPE_PTRMEM_CLASS_TYPE (ptrmem_type));
1716 if (!COMPLETE_TYPE_P (ctype))
1718 if (!same_type_p (ctype, objtype))
1719 goto mismatch;
1720 binfo = NULL;
1722 else
1724 binfo = lookup_base (objtype, ctype, ba_check, NULL, complain);
1726 if (!binfo)
1728 mismatch:
1729 if (complain & tf_error)
1730 error ("pointer to member type %qT incompatible with object "
1731 "type %qT", type, objtype);
1732 return error_mark_node;
1734 else if (binfo == error_mark_node)
1735 return error_mark_node;
1738 if (TYPE_PTRDATAMEM_P (ptrmem_type))
1740 cp_lvalue_kind kind = lvalue_kind (datum);
1741 tree ptype;
1743 /* Compute the type of the field, as described in [expr.ref].
1744 There's no such thing as a mutable pointer-to-member, so
1745 things are not as complex as they are for references to
1746 non-static data members. */
1747 type = cp_build_qualified_type (type,
1748 (cp_type_quals (type)
1749 | cp_type_quals (TREE_TYPE (datum))));
1751 datum = build_address (datum);
1753 /* Convert object to the correct base. */
1754 if (binfo)
1756 datum = build_base_path (PLUS_EXPR, datum, binfo, 1, complain);
1757 if (datum == error_mark_node)
1758 return error_mark_node;
1761 /* Build an expression for "object + offset" where offset is the
1762 value stored in the pointer-to-data-member. */
1763 ptype = build_pointer_type (type);
1764 datum = fold_build_pointer_plus (fold_convert (ptype, datum), component);
1765 datum = cp_build_indirect_ref (datum, RO_NULL, complain);
1766 if (datum == error_mark_node)
1767 return error_mark_node;
1769 /* If the object expression was an rvalue, return an rvalue. */
1770 if (kind & clk_class)
1771 datum = rvalue (datum);
1772 else if (kind & clk_rvalueref)
1773 datum = move (datum);
1774 return datum;
1776 else
1778 /* 5.5/6: In a .* expression whose object expression is an rvalue, the
1779 program is ill-formed if the second operand is a pointer to member
1780 function with ref-qualifier &. In a .* expression whose object
1781 expression is an lvalue, the program is ill-formed if the second
1782 operand is a pointer to member function with ref-qualifier &&. */
1783 if (FUNCTION_REF_QUALIFIED (type))
1785 bool lval = real_lvalue_p (datum);
1786 if (lval && FUNCTION_RVALUE_QUALIFIED (type))
1788 if (complain & tf_error)
1789 error ("pointer-to-member-function type %qT requires an rvalue",
1790 ptrmem_type);
1791 return error_mark_node;
1793 else if (!lval && !FUNCTION_RVALUE_QUALIFIED (type))
1795 if (complain & tf_error)
1796 error ("pointer-to-member-function type %qT requires an lvalue",
1797 ptrmem_type);
1798 return error_mark_node;
1801 return build2 (OFFSET_REF, type, datum, component);
1805 /* Return a tree node for the expression TYPENAME '(' PARMS ')'. */
1807 tree
1808 build_functional_cast (tree exp, tree parms, tsubst_flags_t complain)
1810 /* This is either a call to a constructor,
1811 or a C cast in C++'s `functional' notation. */
1813 /* The type to which we are casting. */
1814 tree type;
1815 vec<tree, va_gc> *parmvec;
1817 if (error_operand_p (exp) || parms == error_mark_node)
1818 return error_mark_node;
1820 if (TREE_CODE (exp) == TYPE_DECL)
1822 type = TREE_TYPE (exp);
1824 if (complain & tf_warning
1825 && TREE_DEPRECATED (type)
1826 && DECL_ARTIFICIAL (exp))
1827 warn_deprecated_use (type, NULL_TREE);
1829 else
1830 type = exp;
1832 /* We need to check this explicitly, since value-initialization of
1833 arrays is allowed in other situations. */
1834 if (TREE_CODE (type) == ARRAY_TYPE)
1836 if (complain & tf_error)
1837 error ("functional cast to array type %qT", type);
1838 return error_mark_node;
1841 if (type_uses_auto (type))
1843 if (complain & tf_error)
1844 error ("invalid use of %<auto%>");
1845 return error_mark_node;
1848 if (processing_template_decl)
1850 tree t;
1852 /* Diagnose this even in a template. We could also try harder
1853 to give all the usual errors when the type and args are
1854 non-dependent... */
1855 if (TREE_CODE (type) == REFERENCE_TYPE && !parms)
1857 if (complain & tf_error)
1858 error ("invalid value-initialization of reference type");
1859 return error_mark_node;
1862 t = build_min (CAST_EXPR, type, parms);
1863 /* We don't know if it will or will not have side effects. */
1864 TREE_SIDE_EFFECTS (t) = 1;
1865 return t;
1868 if (! MAYBE_CLASS_TYPE_P (type))
1870 if (parms == NULL_TREE)
1872 if (VOID_TYPE_P (type))
1873 return void_node;
1874 return build_value_init (cv_unqualified (type), complain);
1877 /* This must build a C cast. */
1878 parms = build_x_compound_expr_from_list (parms, ELK_FUNC_CAST, complain);
1879 return cp_build_c_cast (type, parms, complain);
1882 /* Prepare to evaluate as a call to a constructor. If this expression
1883 is actually used, for example,
1885 return X (arg1, arg2, ...);
1887 then the slot being initialized will be filled in. */
1889 if (!complete_type_or_maybe_complain (type, NULL_TREE, complain))
1890 return error_mark_node;
1891 if (abstract_virtuals_error_sfinae (ACU_CAST, type, complain))
1892 return error_mark_node;
1894 /* [expr.type.conv]
1896 If the expression list is a single-expression, the type
1897 conversion is equivalent (in definedness, and if defined in
1898 meaning) to the corresponding cast expression. */
1899 if (parms && TREE_CHAIN (parms) == NULL_TREE)
1900 return cp_build_c_cast (type, TREE_VALUE (parms), complain);
1902 /* [expr.type.conv]
1904 The expression T(), where T is a simple-type-specifier for a
1905 non-array complete object type or the (possibly cv-qualified)
1906 void type, creates an rvalue of the specified type, which is
1907 value-initialized. */
1909 if (parms == NULL_TREE)
1911 exp = build_value_init (type, complain);
1912 exp = get_target_expr_sfinae (exp, complain);
1913 return exp;
1916 /* Call the constructor. */
1917 parmvec = make_tree_vector ();
1918 for (; parms != NULL_TREE; parms = TREE_CHAIN (parms))
1919 vec_safe_push (parmvec, TREE_VALUE (parms));
1920 exp = build_special_member_call (NULL_TREE, complete_ctor_identifier,
1921 &parmvec, type, LOOKUP_NORMAL, complain);
1922 release_tree_vector (parmvec);
1924 if (exp == error_mark_node)
1925 return error_mark_node;
1927 return build_cplus_new (type, exp, complain);
1931 /* Add new exception specifier SPEC, to the LIST we currently have.
1932 If it's already in LIST then do nothing.
1933 Moan if it's bad and we're allowed to. COMPLAIN < 0 means we
1934 know what we're doing. */
1936 tree
1937 add_exception_specifier (tree list, tree spec, int complain)
1939 bool ok;
1940 tree core = spec;
1941 bool is_ptr;
1942 diagnostic_t diag_type = DK_UNSPECIFIED; /* none */
1944 if (spec == error_mark_node)
1945 return list;
1947 gcc_assert (spec && (!list || TREE_VALUE (list)));
1949 /* [except.spec] 1, type in an exception specifier shall not be
1950 incomplete, or pointer or ref to incomplete other than pointer
1951 to cv void. */
1952 is_ptr = TYPE_PTR_P (core);
1953 if (is_ptr || TREE_CODE (core) == REFERENCE_TYPE)
1954 core = TREE_TYPE (core);
1955 if (complain < 0)
1956 ok = true;
1957 else if (VOID_TYPE_P (core))
1958 ok = is_ptr;
1959 else if (TREE_CODE (core) == TEMPLATE_TYPE_PARM)
1960 ok = true;
1961 else if (processing_template_decl)
1962 ok = true;
1963 else
1965 ok = true;
1966 /* 15.4/1 says that types in an exception specifier must be complete,
1967 but it seems more reasonable to only require this on definitions
1968 and calls. So just give a pedwarn at this point; we will give an
1969 error later if we hit one of those two cases. */
1970 if (!COMPLETE_TYPE_P (complete_type (core)))
1971 diag_type = DK_PEDWARN; /* pedwarn */
1974 if (ok)
1976 tree probe;
1978 for (probe = list; probe; probe = TREE_CHAIN (probe))
1979 if (same_type_p (TREE_VALUE (probe), spec))
1980 break;
1981 if (!probe)
1982 list = tree_cons (NULL_TREE, spec, list);
1984 else
1985 diag_type = DK_ERROR; /* error */
1987 if (diag_type != DK_UNSPECIFIED
1988 && (complain & tf_warning_or_error))
1989 cxx_incomplete_type_diagnostic (NULL_TREE, core, diag_type);
1991 return list;
1994 /* Like nothrow_spec_p, but don't abort on deferred noexcept. */
1996 static bool
1997 nothrow_spec_p_uninst (const_tree spec)
1999 if (DEFERRED_NOEXCEPT_SPEC_P (spec))
2000 return false;
2001 return nothrow_spec_p (spec);
2004 /* Combine the two exceptions specifier lists LIST and ADD, and return
2005 their union. */
2007 tree
2008 merge_exception_specifiers (tree list, tree add)
2010 tree noex, orig_list;
2012 /* No exception-specifier or noexcept(false) are less strict than
2013 anything else. Prefer the newer variant (LIST). */
2014 if (!list || list == noexcept_false_spec)
2015 return list;
2016 else if (!add || add == noexcept_false_spec)
2017 return add;
2019 /* noexcept(true) and throw() are stricter than anything else.
2020 As above, prefer the more recent one (LIST). */
2021 if (nothrow_spec_p_uninst (add))
2022 return list;
2024 /* Two implicit noexcept specs (e.g. on a destructor) are equivalent. */
2025 if (UNEVALUATED_NOEXCEPT_SPEC_P (add)
2026 && UNEVALUATED_NOEXCEPT_SPEC_P (list))
2027 return list;
2028 /* We should have instantiated other deferred noexcept specs by now. */
2029 gcc_assert (!DEFERRED_NOEXCEPT_SPEC_P (add));
2031 if (nothrow_spec_p_uninst (list))
2032 return add;
2033 noex = TREE_PURPOSE (list);
2034 gcc_checking_assert (!TREE_PURPOSE (add)
2035 || cp_tree_equal (noex, TREE_PURPOSE (add)));
2037 /* Combine the dynamic-exception-specifiers, if any. */
2038 orig_list = list;
2039 for (; add && TREE_VALUE (add); add = TREE_CHAIN (add))
2041 tree spec = TREE_VALUE (add);
2042 tree probe;
2044 for (probe = orig_list; probe && TREE_VALUE (probe);
2045 probe = TREE_CHAIN (probe))
2046 if (same_type_p (TREE_VALUE (probe), spec))
2047 break;
2048 if (!probe)
2050 spec = build_tree_list (NULL_TREE, spec);
2051 TREE_CHAIN (spec) = list;
2052 list = spec;
2056 /* Keep the noexcept-specifier at the beginning of the list. */
2057 if (noex != TREE_PURPOSE (list))
2058 list = tree_cons (noex, TREE_VALUE (list), TREE_CHAIN (list));
2060 return list;
2063 /* Subroutine of build_call. Ensure that each of the types in the
2064 exception specification is complete. Technically, 15.4/1 says that
2065 they need to be complete when we see a declaration of the function,
2066 but we should be able to get away with only requiring this when the
2067 function is defined or called. See also add_exception_specifier. */
2069 void
2070 require_complete_eh_spec_types (tree fntype, tree decl)
2072 tree raises;
2073 /* Don't complain about calls to op new. */
2074 if (decl && DECL_ARTIFICIAL (decl))
2075 return;
2076 for (raises = TYPE_RAISES_EXCEPTIONS (fntype); raises;
2077 raises = TREE_CHAIN (raises))
2079 tree type = TREE_VALUE (raises);
2080 if (type && !COMPLETE_TYPE_P (type))
2082 if (decl)
2083 error
2084 ("call to function %qD which throws incomplete type %q#T",
2085 decl, type);
2086 else
2087 error ("call to function which throws incomplete type %q#T",
2088 decl);
2094 #include "gt-cp-typeck2.h"