compiler: Remove obsolete hidden_fields_are_ok code.
[official-gcc.git] / gcc / cp / typeck2.c
blobad69668765a60d5b74da880933e7a66e65ff902c
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++0x 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 = fold_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);
811 value = maybe_constant_init (value);
812 const_init = (reduced_constant_expression_p (value)
813 || error_operand_p (value));
814 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = const_init;
815 TREE_CONSTANT (decl) = const_init && decl_maybe_constant_var_p (decl);
818 /* If the initializer is not a constant, fill in DECL_INITIAL with
819 the bits that are constant, and then return an expression that
820 will perform the dynamic initialization. */
821 if (value != error_mark_node
822 && (TREE_SIDE_EFFECTS (value)
823 || array_of_runtime_bound_p (type)
824 || ! reduced_constant_expression_p (value)))
826 if (TREE_CODE (type) == ARRAY_TYPE
827 && (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (type))
828 || array_of_runtime_bound_p (type)))
829 /* For an array, we only need/want a single cleanup region rather
830 than one per element. */
831 return build_vec_init (decl, NULL_TREE, value, false, 1,
832 tf_warning_or_error);
833 else
834 return split_nonconstant_init (decl, value);
836 /* If the value is a constant, just put it in DECL_INITIAL. If DECL
837 is an automatic variable, the middle end will turn this into a
838 dynamic initialization later. */
839 DECL_INITIAL (decl) = value;
840 return NULL_TREE;
844 /* Give diagnostic about narrowing conversions within { }. */
846 bool
847 check_narrowing (tree type, tree init, tsubst_flags_t complain)
849 tree ftype = unlowered_expr_type (init);
850 bool ok = true;
851 REAL_VALUE_TYPE d;
853 if (((!warn_narrowing || !(complain & tf_warning))
854 && cxx_dialect == cxx98)
855 || !ARITHMETIC_TYPE_P (type))
856 return ok;
858 if (BRACE_ENCLOSED_INITIALIZER_P (init)
859 && TREE_CODE (type) == COMPLEX_TYPE)
861 tree elttype = TREE_TYPE (type);
862 if (CONSTRUCTOR_NELTS (init) > 0)
863 ok &= check_narrowing (elttype, CONSTRUCTOR_ELT (init, 0)->value,
864 complain);
865 if (CONSTRUCTOR_NELTS (init) > 1)
866 ok &= check_narrowing (elttype, CONSTRUCTOR_ELT (init, 1)->value,
867 complain);
868 return ok;
871 init = maybe_constant_value (fold_non_dependent_expr_sfinae (init, tf_none));
873 if (TREE_CODE (type) == INTEGER_TYPE
874 && TREE_CODE (ftype) == REAL_TYPE)
875 ok = false;
876 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype)
877 && CP_INTEGRAL_TYPE_P (type))
879 if (TREE_CODE (ftype) == ENUMERAL_TYPE)
880 /* Check for narrowing based on the values of the enumeration. */
881 ftype = ENUM_UNDERLYING_TYPE (ftype);
882 if ((tree_int_cst_lt (TYPE_MAX_VALUE (type),
883 TYPE_MAX_VALUE (ftype))
884 || tree_int_cst_lt (TYPE_MIN_VALUE (ftype),
885 TYPE_MIN_VALUE (type)))
886 && (TREE_CODE (init) != INTEGER_CST
887 || !int_fits_type_p (init, type)))
888 ok = false;
890 else if (TREE_CODE (ftype) == REAL_TYPE
891 && TREE_CODE (type) == REAL_TYPE)
893 if (TYPE_PRECISION (type) < TYPE_PRECISION (ftype))
895 if (TREE_CODE (init) == REAL_CST)
897 /* Issue 703: Loss of precision is OK as long as the value is
898 within the representable range of the new type. */
899 REAL_VALUE_TYPE r;
900 d = TREE_REAL_CST (init);
901 real_convert (&r, TYPE_MODE (type), &d);
902 if (real_isinf (&r))
903 ok = false;
905 else
906 ok = false;
909 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype)
910 && TREE_CODE (type) == REAL_TYPE)
912 ok = false;
913 if (TREE_CODE (init) == INTEGER_CST)
915 d = real_value_from_int_cst (0, init);
916 if (exact_real_truncate (TYPE_MODE (type), &d))
917 ok = true;
921 if (!ok)
923 if (cxx_dialect == cxx98)
924 warning_at (EXPR_LOC_OR_LOC (init, input_location), OPT_Wnarrowing,
925 "narrowing conversion of %qE from %qT to %qT inside { } "
926 "is ill-formed in C++11", init, ftype, type);
927 else if (!TREE_CONSTANT (init))
929 if (complain & tf_warning_or_error)
931 pedwarn (EXPR_LOC_OR_LOC (init, input_location), OPT_Wnarrowing,
932 "narrowing conversion of %qE from %qT to %qT inside { }",
933 init, ftype, type);
934 ok = true;
937 else if (complain & tf_error)
938 error_at (EXPR_LOC_OR_LOC (init, input_location),
939 "narrowing conversion of %qE from %qT to %qT inside { }",
940 init, ftype, type);
943 return cxx_dialect == cxx98 || ok;
946 /* Process the initializer INIT for a variable of type TYPE, emitting
947 diagnostics for invalid initializers and converting the initializer as
948 appropriate.
950 For aggregate types, it assumes that reshape_init has already run, thus the
951 initializer will have the right shape (brace elision has been undone).
953 NESTED is true iff we are being called for an element of a CONSTRUCTOR. */
955 static tree
956 digest_init_r (tree type, tree init, bool nested, int flags,
957 tsubst_flags_t complain)
959 enum tree_code code = TREE_CODE (type);
961 if (error_operand_p (init))
962 return error_mark_node;
964 gcc_assert (init);
966 /* We must strip the outermost array type when completing the type,
967 because the its bounds might be incomplete at the moment. */
968 if (!complete_type_or_maybe_complain (TREE_CODE (type) == ARRAY_TYPE
969 ? TREE_TYPE (type) : type, NULL_TREE,
970 complain))
971 return error_mark_node;
973 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue
974 (g++.old-deja/g++.law/casts2.C). */
975 if (TREE_CODE (init) == NON_LVALUE_EXPR)
976 init = TREE_OPERAND (init, 0);
978 /* Initialization of an array of chars from a string constant. The initializer
979 can be optionally enclosed in braces, but reshape_init has already removed
980 them if they were present. */
981 if (code == ARRAY_TYPE)
983 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
984 if (char_type_p (typ1)
985 /*&& init */
986 && TREE_CODE (init) == STRING_CST)
988 tree char_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (init)));
990 if (TYPE_PRECISION (typ1) == BITS_PER_UNIT)
992 if (char_type != char_type_node)
994 if (complain & tf_error)
995 error ("char-array initialized from wide string");
996 return error_mark_node;
999 else
1001 if (char_type == char_type_node)
1003 if (complain & tf_error)
1004 error ("int-array initialized from non-wide string");
1005 return error_mark_node;
1007 else if (char_type != typ1)
1009 if (complain & tf_error)
1010 error ("int-array initialized from incompatible "
1011 "wide string");
1012 return error_mark_node;
1016 if (type != TREE_TYPE (init))
1018 init = copy_node (init);
1019 TREE_TYPE (init) = type;
1021 if (TYPE_DOMAIN (type) != 0 && TREE_CONSTANT (TYPE_SIZE (type)))
1023 int size = TREE_INT_CST_LOW (TYPE_SIZE (type));
1024 size = (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
1025 /* In C it is ok to subtract 1 from the length of the string
1026 because it's ok to ignore the terminating null char that is
1027 counted in the length of the constant, but in C++ this would
1028 be invalid. */
1029 if (size < TREE_STRING_LENGTH (init))
1030 permerror (input_location, "initializer-string for array "
1031 "of chars is too long");
1033 return init;
1037 /* Handle scalar types (including conversions) and references. */
1038 if ((TREE_CODE (type) != COMPLEX_TYPE
1039 || BRACE_ENCLOSED_INITIALIZER_P (init))
1040 && (SCALAR_TYPE_P (type) || code == REFERENCE_TYPE))
1042 tree *exp;
1044 if (nested)
1045 flags |= LOOKUP_NO_NARROWING;
1046 init = convert_for_initialization (0, type, init, flags,
1047 ICR_INIT, NULL_TREE, 0,
1048 complain);
1049 exp = &init;
1051 /* Skip any conversions since we'll be outputting the underlying
1052 constant. */
1053 while (CONVERT_EXPR_P (*exp)
1054 || TREE_CODE (*exp) == NON_LVALUE_EXPR)
1055 exp = &TREE_OPERAND (*exp, 0);
1057 *exp = cplus_expand_constant (*exp);
1059 return init;
1062 /* Come here only for aggregates: records, arrays, unions, complex numbers
1063 and vectors. */
1064 gcc_assert (TREE_CODE (type) == ARRAY_TYPE
1065 || TREE_CODE (type) == VECTOR_TYPE
1066 || TREE_CODE (type) == RECORD_TYPE
1067 || TREE_CODE (type) == UNION_TYPE
1068 || TREE_CODE (type) == COMPLEX_TYPE);
1070 if (BRACE_ENCLOSED_INITIALIZER_P (init)
1071 && !TYPE_NON_AGGREGATE_CLASS (type))
1072 return process_init_constructor (type, init, complain);
1073 else
1075 if (COMPOUND_LITERAL_P (init) && TREE_CODE (type) == ARRAY_TYPE)
1077 if (complain & tf_error)
1078 error ("cannot initialize aggregate of type %qT with "
1079 "a compound literal", type);
1081 return error_mark_node;
1084 if (TREE_CODE (type) == ARRAY_TYPE
1085 && !BRACE_ENCLOSED_INITIALIZER_P (init))
1087 /* Allow the result of build_array_copy and of
1088 build_value_init_noctor. */
1089 if ((TREE_CODE (init) == VEC_INIT_EXPR
1090 || TREE_CODE (init) == CONSTRUCTOR)
1091 && (same_type_ignoring_top_level_qualifiers_p
1092 (type, TREE_TYPE (init))))
1093 return init;
1095 if (complain & tf_error)
1096 error ("array must be initialized with a brace-enclosed"
1097 " initializer");
1098 return error_mark_node;
1101 return convert_for_initialization (NULL_TREE, type, init,
1102 flags,
1103 ICR_INIT, NULL_TREE, 0,
1104 complain);
1108 tree
1109 digest_init (tree type, tree init, tsubst_flags_t complain)
1111 return digest_init_r (type, init, false, LOOKUP_IMPLICIT, complain);
1114 tree
1115 digest_init_flags (tree type, tree init, int flags)
1117 return digest_init_r (type, init, false, flags, tf_warning_or_error);
1120 /* Process the initializer INIT for an NSDMI DECL (a FIELD_DECL). */
1121 tree
1122 digest_nsdmi_init (tree decl, tree init)
1124 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
1126 int flags = LOOKUP_IMPLICIT;
1127 if (DIRECT_LIST_INIT_P (init))
1128 flags = LOOKUP_NORMAL;
1129 init = digest_init_flags (TREE_TYPE (decl), init, flags);
1130 if (TREE_CODE (init) == TARGET_EXPR)
1131 /* This represents the whole initialization. */
1132 TARGET_EXPR_DIRECT_INIT_P (init) = true;
1133 return init;
1136 /* Set of flags used within process_init_constructor to describe the
1137 initializers. */
1138 #define PICFLAG_ERRONEOUS 1
1139 #define PICFLAG_NOT_ALL_CONSTANT 2
1140 #define PICFLAG_NOT_ALL_SIMPLE 4
1141 #define PICFLAG_SIDE_EFFECTS 8
1143 /* Given an initializer INIT, return the flag (PICFLAG_*) which better
1144 describe it. */
1146 static int
1147 picflag_from_initializer (tree init)
1149 if (init == error_mark_node)
1150 return PICFLAG_ERRONEOUS;
1151 else if (!TREE_CONSTANT (init))
1153 if (TREE_SIDE_EFFECTS (init))
1154 return PICFLAG_SIDE_EFFECTS;
1155 else
1156 return PICFLAG_NOT_ALL_CONSTANT;
1158 else if (!initializer_constant_valid_p (init, TREE_TYPE (init)))
1159 return PICFLAG_NOT_ALL_SIMPLE;
1160 return 0;
1163 /* Adjust INIT for going into a CONSTRUCTOR. */
1165 static tree
1166 massage_init_elt (tree type, tree init, tsubst_flags_t complain)
1168 init = digest_init_r (type, init, true, LOOKUP_IMPLICIT, complain);
1169 /* Strip a simple TARGET_EXPR when we know this is an initializer. */
1170 if (TREE_CODE (init) == TARGET_EXPR
1171 && !VOID_TYPE_P (TREE_TYPE (TARGET_EXPR_INITIAL (init))))
1172 init = TARGET_EXPR_INITIAL (init);
1173 /* When we defer constant folding within a statement, we may want to
1174 defer this folding as well. */
1175 tree t = fold_non_dependent_expr_sfinae (init, complain);
1176 t = maybe_constant_init (t);
1177 if (TREE_CONSTANT (t))
1178 init = t;
1179 return init;
1182 /* Subroutine of process_init_constructor, which will process an initializer
1183 INIT for an array or vector of type TYPE. Returns the flags (PICFLAG_*)
1184 which describe the initializers. */
1186 static int
1187 process_init_constructor_array (tree type, tree init,
1188 tsubst_flags_t complain)
1190 unsigned HOST_WIDE_INT i, len = 0;
1191 int flags = 0;
1192 bool unbounded = false;
1193 constructor_elt *ce;
1194 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (init);
1196 gcc_assert (TREE_CODE (type) == ARRAY_TYPE
1197 || TREE_CODE (type) == VECTOR_TYPE);
1199 if (TREE_CODE (type) == ARRAY_TYPE)
1201 tree domain = TYPE_DOMAIN (type);
1202 if (domain && TREE_CONSTANT (TYPE_MAX_VALUE (domain)))
1203 len = wi::ext (wi::to_offset (TYPE_MAX_VALUE (domain))
1204 - wi::to_offset (TYPE_MIN_VALUE (domain)) + 1,
1205 TYPE_PRECISION (TREE_TYPE (domain)),
1206 TYPE_SIGN (TREE_TYPE (domain))).to_uhwi ();
1207 else
1208 unbounded = true; /* Take as many as there are. */
1210 else
1211 /* Vectors are like simple fixed-size arrays. */
1212 len = TYPE_VECTOR_SUBPARTS (type);
1214 /* There must not be more initializers than needed. */
1215 if (!unbounded && vec_safe_length (v) > len)
1217 if (complain & tf_error)
1218 error ("too many initializers for %qT", type);
1219 else
1220 return PICFLAG_ERRONEOUS;
1223 FOR_EACH_VEC_SAFE_ELT (v, i, ce)
1225 if (ce->index)
1227 gcc_assert (TREE_CODE (ce->index) == INTEGER_CST);
1228 if (compare_tree_int (ce->index, i) != 0)
1230 ce->value = error_mark_node;
1231 sorry ("non-trivial designated initializers not supported");
1234 else
1235 ce->index = size_int (i);
1236 gcc_assert (ce->value);
1237 ce->value = massage_init_elt (TREE_TYPE (type), ce->value, complain);
1239 if (ce->value != error_mark_node)
1240 gcc_assert (same_type_ignoring_top_level_qualifiers_p
1241 (TREE_TYPE (type), TREE_TYPE (ce->value)));
1243 flags |= picflag_from_initializer (ce->value);
1246 /* No more initializers. If the array is unbounded, we are done. Otherwise,
1247 we must add initializers ourselves. */
1248 if (!unbounded)
1249 for (; i < len; ++i)
1251 tree next;
1253 if (type_build_ctor_call (TREE_TYPE (type)))
1255 /* If this type needs constructors run for default-initialization,
1256 we can't rely on the back end to do it for us, so make the
1257 initialization explicit by list-initializing from T{}. */
1258 next = build_constructor (init_list_type_node, NULL);
1259 CONSTRUCTOR_IS_DIRECT_INIT (next) = true;
1260 next = massage_init_elt (TREE_TYPE (type), next, complain);
1261 if (initializer_zerop (next))
1262 /* The default zero-initialization is fine for us; don't
1263 add anything to the CONSTRUCTOR. */
1264 next = NULL_TREE;
1266 else if (!zero_init_p (TREE_TYPE (type)))
1267 next = build_zero_init (TREE_TYPE (type),
1268 /*nelts=*/NULL_TREE,
1269 /*static_storage_p=*/false);
1270 else
1271 /* The default zero-initialization is fine for us; don't
1272 add anything to the CONSTRUCTOR. */
1273 next = NULL_TREE;
1275 if (next)
1277 flags |= picflag_from_initializer (next);
1278 CONSTRUCTOR_APPEND_ELT (v, size_int (i), next);
1282 CONSTRUCTOR_ELTS (init) = v;
1283 return flags;
1286 /* Subroutine of process_init_constructor, which will process an initializer
1287 INIT for a class of type TYPE. Returns the flags (PICFLAG_*) which describe
1288 the initializers. */
1290 static int
1291 process_init_constructor_record (tree type, tree init,
1292 tsubst_flags_t complain)
1294 vec<constructor_elt, va_gc> *v = NULL;
1295 int flags = 0;
1296 tree field;
1297 unsigned HOST_WIDE_INT idx = 0;
1299 gcc_assert (TREE_CODE (type) == RECORD_TYPE);
1300 gcc_assert (!CLASSTYPE_VBASECLASSES (type));
1301 gcc_assert (!TYPE_BINFO (type)
1302 || !BINFO_N_BASE_BINFOS (TYPE_BINFO (type)));
1303 gcc_assert (!TYPE_POLYMORPHIC_P (type));
1305 /* Generally, we will always have an index for each initializer (which is
1306 a FIELD_DECL, put by reshape_init), but compound literals don't go trough
1307 reshape_init. So we need to handle both cases. */
1308 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
1310 tree next;
1311 tree type;
1313 if (!DECL_NAME (field) && DECL_C_BIT_FIELD (field))
1314 continue;
1316 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
1317 continue;
1319 /* If this is a bitfield, first convert to the declared type. */
1320 type = TREE_TYPE (field);
1321 if (DECL_BIT_FIELD_TYPE (field))
1322 type = DECL_BIT_FIELD_TYPE (field);
1323 if (type == error_mark_node)
1324 return PICFLAG_ERRONEOUS;
1326 if (idx < vec_safe_length (CONSTRUCTOR_ELTS (init)))
1328 constructor_elt *ce = &(*CONSTRUCTOR_ELTS (init))[idx];
1329 if (ce->index)
1331 /* We can have either a FIELD_DECL or an IDENTIFIER_NODE. The
1332 latter case can happen in templates where lookup has to be
1333 deferred. */
1334 gcc_assert (TREE_CODE (ce->index) == FIELD_DECL
1335 || identifier_p (ce->index));
1336 if (ce->index != field
1337 && ce->index != DECL_NAME (field))
1339 ce->value = error_mark_node;
1340 sorry ("non-trivial designated initializers not supported");
1344 gcc_assert (ce->value);
1345 next = massage_init_elt (type, ce->value, complain);
1346 ++idx;
1348 else if (type_build_ctor_call (TREE_TYPE (field)))
1350 /* If this type needs constructors run for
1351 default-initialization, we can't rely on the back end to do it
1352 for us, so build up TARGET_EXPRs. If the type in question is
1353 a class, just build one up; if it's an array, recurse. */
1354 next = build_constructor (init_list_type_node, NULL);
1355 /* Call this direct-initialization pending DR 1518 resolution so
1356 that explicit default ctors don't break valid C++03 code. */
1357 CONSTRUCTOR_IS_DIRECT_INIT (next) = true;
1358 next = massage_init_elt (TREE_TYPE (field), next, complain);
1360 /* Warn when some struct elements are implicitly initialized. */
1361 if ((complain & tf_warning)
1362 && !EMPTY_CONSTRUCTOR_P (init))
1363 warning (OPT_Wmissing_field_initializers,
1364 "missing initializer for member %qD", field);
1366 else
1368 if (TREE_CODE (TREE_TYPE (field)) == REFERENCE_TYPE)
1370 if (complain & tf_error)
1371 error ("member %qD is uninitialized reference", field);
1372 else
1373 return PICFLAG_ERRONEOUS;
1375 else if (CLASSTYPE_REF_FIELDS_NEED_INIT (TREE_TYPE (field)))
1377 if (complain & tf_error)
1378 error ("member %qD with uninitialized reference fields", field);
1379 else
1380 return PICFLAG_ERRONEOUS;
1383 /* Warn when some struct elements are implicitly initialized
1384 to zero. */
1385 if ((complain & tf_warning)
1386 && !EMPTY_CONSTRUCTOR_P (init))
1387 warning (OPT_Wmissing_field_initializers,
1388 "missing initializer for member %qD", field);
1390 if (!zero_init_p (TREE_TYPE (field)))
1391 next = build_zero_init (TREE_TYPE (field), /*nelts=*/NULL_TREE,
1392 /*static_storage_p=*/false);
1393 else
1394 /* The default zero-initialization is fine for us; don't
1395 add anything to the CONSTRUCTOR. */
1396 continue;
1399 /* If this is a bitfield, now convert to the lowered type. */
1400 if (type != TREE_TYPE (field))
1401 next = cp_convert_and_check (TREE_TYPE (field), next, complain);
1402 flags |= picflag_from_initializer (next);
1403 CONSTRUCTOR_APPEND_ELT (v, field, next);
1406 if (idx < vec_safe_length (CONSTRUCTOR_ELTS (init)))
1408 if (complain & tf_error)
1409 error ("too many initializers for %qT", type);
1410 else
1411 return PICFLAG_ERRONEOUS;
1414 CONSTRUCTOR_ELTS (init) = v;
1415 return flags;
1418 /* Subroutine of process_init_constructor, which will process a single
1419 initializer INIT for a union of type TYPE. Returns the flags (PICFLAG_*)
1420 which describe the initializer. */
1422 static int
1423 process_init_constructor_union (tree type, tree init,
1424 tsubst_flags_t complain)
1426 constructor_elt *ce;
1427 int len;
1429 /* If the initializer was empty, use default zero initialization. */
1430 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
1431 return 0;
1433 len = CONSTRUCTOR_ELTS (init)->length ();
1434 if (len > 1)
1436 if (!(complain & tf_error))
1437 return PICFLAG_ERRONEOUS;
1438 error ("too many initializers for %qT", type);
1439 CONSTRUCTOR_ELTS (init)->block_remove (1, len-1);
1442 ce = &(*CONSTRUCTOR_ELTS (init))[0];
1444 /* If this element specifies a field, initialize via that field. */
1445 if (ce->index)
1447 if (TREE_CODE (ce->index) == FIELD_DECL)
1449 else if (identifier_p (ce->index))
1451 /* This can happen within a cast, see g++.dg/opt/cse2.C. */
1452 tree name = ce->index;
1453 tree field;
1454 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1455 if (DECL_NAME (field) == name)
1456 break;
1457 if (!field)
1459 if (complain & tf_error)
1460 error ("no field %qD found in union being initialized",
1461 field);
1462 ce->value = error_mark_node;
1464 ce->index = field;
1466 else
1468 gcc_assert (TREE_CODE (ce->index) == INTEGER_CST
1469 || TREE_CODE (ce->index) == RANGE_EXPR);
1470 if (complain & tf_error)
1471 error ("index value instead of field name in union initializer");
1472 ce->value = error_mark_node;
1475 else
1477 /* Find the first named field. ANSI decided in September 1990
1478 that only named fields count here. */
1479 tree field = TYPE_FIELDS (type);
1480 while (field && (!DECL_NAME (field) || TREE_CODE (field) != FIELD_DECL))
1481 field = TREE_CHAIN (field);
1482 if (field == NULL_TREE)
1484 if (complain & tf_error)
1485 error ("too many initializers for %qT", type);
1486 ce->value = error_mark_node;
1488 ce->index = field;
1491 if (ce->value && ce->value != error_mark_node)
1492 ce->value = massage_init_elt (TREE_TYPE (ce->index), ce->value, complain);
1494 return picflag_from_initializer (ce->value);
1497 /* Process INIT, a constructor for a variable of aggregate type TYPE. The
1498 constructor is a brace-enclosed initializer, and will be modified in-place.
1500 Each element is converted to the right type through digest_init, and
1501 missing initializers are added following the language rules (zero-padding,
1502 etc.).
1504 After the execution, the initializer will have TREE_CONSTANT if all elts are
1505 constant, and TREE_STATIC set if, in addition, all elts are simple enough
1506 constants that the assembler and linker can compute them.
1508 The function returns the initializer itself, or error_mark_node in case
1509 of error. */
1511 static tree
1512 process_init_constructor (tree type, tree init, tsubst_flags_t complain)
1514 int flags;
1516 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
1518 if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == VECTOR_TYPE)
1519 flags = process_init_constructor_array (type, init, complain);
1520 else if (TREE_CODE (type) == RECORD_TYPE)
1521 flags = process_init_constructor_record (type, init, complain);
1522 else if (TREE_CODE (type) == UNION_TYPE)
1523 flags = process_init_constructor_union (type, init, complain);
1524 else
1525 gcc_unreachable ();
1527 if (flags & PICFLAG_ERRONEOUS)
1528 return error_mark_node;
1530 TREE_TYPE (init) = type;
1531 if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == NULL_TREE)
1532 cp_complete_array_type (&TREE_TYPE (init), init, /*do_default=*/0);
1533 if (flags & PICFLAG_SIDE_EFFECTS)
1535 TREE_CONSTANT (init) = false;
1536 TREE_SIDE_EFFECTS (init) = true;
1538 else if (flags & PICFLAG_NOT_ALL_CONSTANT)
1539 /* Make sure TREE_CONSTANT isn't set from build_constructor. */
1540 TREE_CONSTANT (init) = false;
1541 else
1543 TREE_CONSTANT (init) = 1;
1544 if (!(flags & PICFLAG_NOT_ALL_SIMPLE))
1545 TREE_STATIC (init) = 1;
1547 return init;
1550 /* Given a structure or union value DATUM, construct and return
1551 the structure or union component which results from narrowing
1552 that value to the base specified in BASETYPE. For example, given the
1553 hierarchy
1555 class L { int ii; };
1556 class A : L { ... };
1557 class B : L { ... };
1558 class C : A, B { ... };
1560 and the declaration
1562 C x;
1564 then the expression
1566 x.A::ii refers to the ii member of the L part of
1567 the A part of the C object named by X. In this case,
1568 DATUM would be x, and BASETYPE would be A.
1570 I used to think that this was nonconformant, that the standard specified
1571 that first we look up ii in A, then convert x to an L& and pull out the
1572 ii part. But in fact, it does say that we convert x to an A&; A here
1573 is known as the "naming class". (jason 2000-12-19)
1575 BINFO_P points to a variable initialized either to NULL_TREE or to the
1576 binfo for the specific base subobject we want to convert to. */
1578 tree
1579 build_scoped_ref (tree datum, tree basetype, tree* binfo_p)
1581 tree binfo;
1583 if (datum == error_mark_node)
1584 return error_mark_node;
1585 if (*binfo_p)
1586 binfo = *binfo_p;
1587 else
1588 binfo = lookup_base (TREE_TYPE (datum), basetype, ba_check,
1589 NULL, tf_warning_or_error);
1591 if (!binfo || binfo == error_mark_node)
1593 *binfo_p = NULL_TREE;
1594 if (!binfo)
1595 error_not_base_type (basetype, TREE_TYPE (datum));
1596 return error_mark_node;
1599 *binfo_p = binfo;
1600 return build_base_path (PLUS_EXPR, datum, binfo, 1,
1601 tf_warning_or_error);
1604 /* Build a reference to an object specified by the C++ `->' operator.
1605 Usually this just involves dereferencing the object, but if the
1606 `->' operator is overloaded, then such overloads must be
1607 performed until an object which does not have the `->' operator
1608 overloaded is found. An error is reported when circular pointer
1609 delegation is detected. */
1611 tree
1612 build_x_arrow (location_t loc, tree expr, tsubst_flags_t complain)
1614 tree orig_expr = expr;
1615 tree type = TREE_TYPE (expr);
1616 tree last_rval = NULL_TREE;
1617 vec<tree, va_gc> *types_memoized = NULL;
1619 if (type == error_mark_node)
1620 return error_mark_node;
1622 if (processing_template_decl)
1624 if (type_dependent_expression_p (expr))
1625 return build_min_nt_loc (loc, ARROW_EXPR, expr);
1626 expr = build_non_dependent_expr (expr);
1629 if (MAYBE_CLASS_TYPE_P (type))
1631 struct tinst_level *actual_inst = current_instantiation ();
1632 tree fn = NULL;
1634 while ((expr = build_new_op (loc, COMPONENT_REF,
1635 LOOKUP_NORMAL, expr, NULL_TREE, NULL_TREE,
1636 &fn, complain)))
1638 if (expr == error_mark_node)
1639 return error_mark_node;
1641 /* This provides a better instantiation backtrace in case of
1642 error. */
1643 if (fn && DECL_USE_TEMPLATE (fn))
1644 push_tinst_level_loc (fn,
1645 (current_instantiation () != actual_inst)
1646 ? DECL_SOURCE_LOCATION (fn)
1647 : input_location);
1648 fn = NULL;
1650 if (vec_member (TREE_TYPE (expr), types_memoized))
1652 if (complain & tf_error)
1653 error ("circular pointer delegation detected");
1654 return error_mark_node;
1657 vec_safe_push (types_memoized, TREE_TYPE (expr));
1658 last_rval = expr;
1661 while (current_instantiation () != actual_inst)
1662 pop_tinst_level ();
1664 if (last_rval == NULL_TREE)
1666 if (complain & tf_error)
1667 error ("base operand of %<->%> has non-pointer type %qT", type);
1668 return error_mark_node;
1671 if (TREE_CODE (TREE_TYPE (last_rval)) == REFERENCE_TYPE)
1672 last_rval = convert_from_reference (last_rval);
1674 else
1675 last_rval = decay_conversion (expr, complain);
1677 if (TYPE_PTR_P (TREE_TYPE (last_rval)))
1679 if (processing_template_decl)
1681 expr = build_min (ARROW_EXPR, TREE_TYPE (TREE_TYPE (last_rval)),
1682 orig_expr);
1683 TREE_SIDE_EFFECTS (expr) = TREE_SIDE_EFFECTS (last_rval);
1684 return expr;
1687 return cp_build_indirect_ref (last_rval, RO_NULL, complain);
1690 if (complain & tf_error)
1692 if (types_memoized)
1693 error ("result of %<operator->()%> yields non-pointer result");
1694 else
1695 error ("base operand of %<->%> is not a pointer");
1697 return error_mark_node;
1700 /* Return an expression for "DATUM .* COMPONENT". DATUM has not
1701 already been checked out to be of aggregate type. */
1703 tree
1704 build_m_component_ref (tree datum, tree component, tsubst_flags_t complain)
1706 tree ptrmem_type;
1707 tree objtype;
1708 tree type;
1709 tree binfo;
1710 tree ctype;
1712 if (error_operand_p (datum) || error_operand_p (component))
1713 return error_mark_node;
1715 datum = mark_lvalue_use (datum);
1716 component = mark_rvalue_use (component);
1718 ptrmem_type = TREE_TYPE (component);
1719 if (!TYPE_PTRMEM_P (ptrmem_type))
1721 if (complain & tf_error)
1722 error ("%qE cannot be used as a member pointer, since it is of "
1723 "type %qT", component, ptrmem_type);
1724 return error_mark_node;
1727 objtype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
1728 if (! MAYBE_CLASS_TYPE_P (objtype))
1730 if (complain & tf_error)
1731 error ("cannot apply member pointer %qE to %qE, which is of "
1732 "non-class type %qT", component, datum, objtype);
1733 return error_mark_node;
1736 type = TYPE_PTRMEM_POINTED_TO_TYPE (ptrmem_type);
1737 ctype = complete_type (TYPE_PTRMEM_CLASS_TYPE (ptrmem_type));
1739 if (!COMPLETE_TYPE_P (ctype))
1741 if (!same_type_p (ctype, objtype))
1742 goto mismatch;
1743 binfo = NULL;
1745 else
1747 binfo = lookup_base (objtype, ctype, ba_check, NULL, complain);
1749 if (!binfo)
1751 mismatch:
1752 if (complain & tf_error)
1753 error ("pointer to member type %qT incompatible with object "
1754 "type %qT", type, objtype);
1755 return error_mark_node;
1757 else if (binfo == error_mark_node)
1758 return error_mark_node;
1761 if (TYPE_PTRDATAMEM_P (ptrmem_type))
1763 cp_lvalue_kind kind = lvalue_kind (datum);
1764 tree ptype;
1766 /* Compute the type of the field, as described in [expr.ref].
1767 There's no such thing as a mutable pointer-to-member, so
1768 things are not as complex as they are for references to
1769 non-static data members. */
1770 type = cp_build_qualified_type (type,
1771 (cp_type_quals (type)
1772 | cp_type_quals (TREE_TYPE (datum))));
1774 datum = build_address (datum);
1776 /* Convert object to the correct base. */
1777 if (binfo)
1779 datum = build_base_path (PLUS_EXPR, datum, binfo, 1, complain);
1780 if (datum == error_mark_node)
1781 return error_mark_node;
1784 /* Build an expression for "object + offset" where offset is the
1785 value stored in the pointer-to-data-member. */
1786 ptype = build_pointer_type (type);
1787 datum = fold_build_pointer_plus (fold_convert (ptype, datum), component);
1788 datum = cp_build_indirect_ref (datum, RO_NULL, complain);
1789 if (datum == error_mark_node)
1790 return error_mark_node;
1792 /* If the object expression was an rvalue, return an rvalue. */
1793 if (kind & clk_class)
1794 datum = rvalue (datum);
1795 else if (kind & clk_rvalueref)
1796 datum = move (datum);
1797 return datum;
1799 else
1801 /* 5.5/6: In a .* expression whose object expression is an rvalue, the
1802 program is ill-formed if the second operand is a pointer to member
1803 function with ref-qualifier &. In a .* expression whose object
1804 expression is an lvalue, the program is ill-formed if the second
1805 operand is a pointer to member function with ref-qualifier &&. */
1806 if (FUNCTION_REF_QUALIFIED (type))
1808 bool lval = real_lvalue_p (datum);
1809 if (lval && FUNCTION_RVALUE_QUALIFIED (type))
1811 if (complain & tf_error)
1812 error ("pointer-to-member-function type %qT requires an rvalue",
1813 ptrmem_type);
1814 return error_mark_node;
1816 else if (!lval && !FUNCTION_RVALUE_QUALIFIED (type))
1818 if (complain & tf_error)
1819 error ("pointer-to-member-function type %qT requires an lvalue",
1820 ptrmem_type);
1821 return error_mark_node;
1824 return build2 (OFFSET_REF, type, datum, component);
1828 /* Return a tree node for the expression TYPENAME '(' PARMS ')'. */
1830 tree
1831 build_functional_cast (tree exp, tree parms, tsubst_flags_t complain)
1833 /* This is either a call to a constructor,
1834 or a C cast in C++'s `functional' notation. */
1836 /* The type to which we are casting. */
1837 tree type;
1838 vec<tree, va_gc> *parmvec;
1840 if (error_operand_p (exp) || parms == error_mark_node)
1841 return error_mark_node;
1843 if (TREE_CODE (exp) == TYPE_DECL)
1845 type = TREE_TYPE (exp);
1847 if (complain & tf_warning
1848 && TREE_DEPRECATED (type)
1849 && DECL_ARTIFICIAL (exp))
1850 warn_deprecated_use (type, NULL_TREE);
1852 else
1853 type = exp;
1855 /* We need to check this explicitly, since value-initialization of
1856 arrays is allowed in other situations. */
1857 if (TREE_CODE (type) == ARRAY_TYPE)
1859 if (complain & tf_error)
1860 error ("functional cast to array type %qT", type);
1861 return error_mark_node;
1864 if (type_uses_auto (type))
1866 if (complain & tf_error)
1867 error ("invalid use of %<auto%>");
1868 return error_mark_node;
1871 if (processing_template_decl)
1873 tree t;
1875 /* Diagnose this even in a template. We could also try harder
1876 to give all the usual errors when the type and args are
1877 non-dependent... */
1878 if (TREE_CODE (type) == REFERENCE_TYPE && !parms)
1880 if (complain & tf_error)
1881 error ("invalid value-initialization of reference type");
1882 return error_mark_node;
1885 t = build_min (CAST_EXPR, type, parms);
1886 /* We don't know if it will or will not have side effects. */
1887 TREE_SIDE_EFFECTS (t) = 1;
1888 return t;
1891 if (! MAYBE_CLASS_TYPE_P (type))
1893 if (parms == NULL_TREE)
1895 if (VOID_TYPE_P (type))
1896 return void_node;
1897 return build_value_init (cv_unqualified (type), complain);
1900 /* This must build a C cast. */
1901 parms = build_x_compound_expr_from_list (parms, ELK_FUNC_CAST, complain);
1902 return cp_build_c_cast (type, parms, complain);
1905 /* Prepare to evaluate as a call to a constructor. If this expression
1906 is actually used, for example,
1908 return X (arg1, arg2, ...);
1910 then the slot being initialized will be filled in. */
1912 if (!complete_type_or_maybe_complain (type, NULL_TREE, complain))
1913 return error_mark_node;
1914 if (abstract_virtuals_error_sfinae (ACU_CAST, type, complain))
1915 return error_mark_node;
1917 /* [expr.type.conv]
1919 If the expression list is a single-expression, the type
1920 conversion is equivalent (in definedness, and if defined in
1921 meaning) to the corresponding cast expression. */
1922 if (parms && TREE_CHAIN (parms) == NULL_TREE)
1923 return cp_build_c_cast (type, TREE_VALUE (parms), complain);
1925 /* [expr.type.conv]
1927 The expression T(), where T is a simple-type-specifier for a
1928 non-array complete object type or the (possibly cv-qualified)
1929 void type, creates an rvalue of the specified type, which is
1930 value-initialized. */
1932 if (parms == NULL_TREE)
1934 exp = build_value_init (type, complain);
1935 exp = get_target_expr_sfinae (exp, complain);
1936 return exp;
1939 /* Call the constructor. */
1940 parmvec = make_tree_vector ();
1941 for (; parms != NULL_TREE; parms = TREE_CHAIN (parms))
1942 vec_safe_push (parmvec, TREE_VALUE (parms));
1943 exp = build_special_member_call (NULL_TREE, complete_ctor_identifier,
1944 &parmvec, type, LOOKUP_NORMAL, complain);
1945 release_tree_vector (parmvec);
1947 if (exp == error_mark_node)
1948 return error_mark_node;
1950 return build_cplus_new (type, exp, complain);
1954 /* Add new exception specifier SPEC, to the LIST we currently have.
1955 If it's already in LIST then do nothing.
1956 Moan if it's bad and we're allowed to. COMPLAIN < 0 means we
1957 know what we're doing. */
1959 tree
1960 add_exception_specifier (tree list, tree spec, int complain)
1962 bool ok;
1963 tree core = spec;
1964 bool is_ptr;
1965 diagnostic_t diag_type = DK_UNSPECIFIED; /* none */
1967 if (spec == error_mark_node)
1968 return list;
1970 gcc_assert (spec && (!list || TREE_VALUE (list)));
1972 /* [except.spec] 1, type in an exception specifier shall not be
1973 incomplete, or pointer or ref to incomplete other than pointer
1974 to cv void. */
1975 is_ptr = TYPE_PTR_P (core);
1976 if (is_ptr || TREE_CODE (core) == REFERENCE_TYPE)
1977 core = TREE_TYPE (core);
1978 if (complain < 0)
1979 ok = true;
1980 else if (VOID_TYPE_P (core))
1981 ok = is_ptr;
1982 else if (TREE_CODE (core) == TEMPLATE_TYPE_PARM)
1983 ok = true;
1984 else if (processing_template_decl)
1985 ok = true;
1986 else
1988 ok = true;
1989 /* 15.4/1 says that types in an exception specifier must be complete,
1990 but it seems more reasonable to only require this on definitions
1991 and calls. So just give a pedwarn at this point; we will give an
1992 error later if we hit one of those two cases. */
1993 if (!COMPLETE_TYPE_P (complete_type (core)))
1994 diag_type = DK_PEDWARN; /* pedwarn */
1997 if (ok)
1999 tree probe;
2001 for (probe = list; probe; probe = TREE_CHAIN (probe))
2002 if (same_type_p (TREE_VALUE (probe), spec))
2003 break;
2004 if (!probe)
2005 list = tree_cons (NULL_TREE, spec, list);
2007 else
2008 diag_type = DK_ERROR; /* error */
2010 if (diag_type != DK_UNSPECIFIED
2011 && (complain & tf_warning_or_error))
2012 cxx_incomplete_type_diagnostic (NULL_TREE, core, diag_type);
2014 return list;
2017 /* Like nothrow_spec_p, but don't abort on deferred noexcept. */
2019 static bool
2020 nothrow_spec_p_uninst (const_tree spec)
2022 if (DEFERRED_NOEXCEPT_SPEC_P (spec))
2023 return false;
2024 return nothrow_spec_p (spec);
2027 /* Combine the two exceptions specifier lists LIST and ADD, and return
2028 their union. */
2030 tree
2031 merge_exception_specifiers (tree list, tree add)
2033 tree noex, orig_list;
2035 /* No exception-specifier or noexcept(false) are less strict than
2036 anything else. Prefer the newer variant (LIST). */
2037 if (!list || list == noexcept_false_spec)
2038 return list;
2039 else if (!add || add == noexcept_false_spec)
2040 return add;
2042 /* noexcept(true) and throw() are stricter than anything else.
2043 As above, prefer the more recent one (LIST). */
2044 if (nothrow_spec_p_uninst (add))
2045 return list;
2047 /* Two implicit noexcept specs (e.g. on a destructor) are equivalent. */
2048 if (UNEVALUATED_NOEXCEPT_SPEC_P (add)
2049 && UNEVALUATED_NOEXCEPT_SPEC_P (list))
2050 return list;
2051 /* We should have instantiated other deferred noexcept specs by now. */
2052 gcc_assert (!DEFERRED_NOEXCEPT_SPEC_P (add));
2054 if (nothrow_spec_p_uninst (list))
2055 return add;
2056 noex = TREE_PURPOSE (list);
2057 gcc_checking_assert (!TREE_PURPOSE (add)
2058 || cp_tree_equal (noex, TREE_PURPOSE (add)));
2060 /* Combine the dynamic-exception-specifiers, if any. */
2061 orig_list = list;
2062 for (; add && TREE_VALUE (add); add = TREE_CHAIN (add))
2064 tree spec = TREE_VALUE (add);
2065 tree probe;
2067 for (probe = orig_list; probe && TREE_VALUE (probe);
2068 probe = TREE_CHAIN (probe))
2069 if (same_type_p (TREE_VALUE (probe), spec))
2070 break;
2071 if (!probe)
2073 spec = build_tree_list (NULL_TREE, spec);
2074 TREE_CHAIN (spec) = list;
2075 list = spec;
2079 /* Keep the noexcept-specifier at the beginning of the list. */
2080 if (noex != TREE_PURPOSE (list))
2081 list = tree_cons (noex, TREE_VALUE (list), TREE_CHAIN (list));
2083 return list;
2086 /* Subroutine of build_call. Ensure that each of the types in the
2087 exception specification is complete. Technically, 15.4/1 says that
2088 they need to be complete when we see a declaration of the function,
2089 but we should be able to get away with only requiring this when the
2090 function is defined or called. See also add_exception_specifier. */
2092 void
2093 require_complete_eh_spec_types (tree fntype, tree decl)
2095 tree raises;
2096 /* Don't complain about calls to op new. */
2097 if (decl && DECL_ARTIFICIAL (decl))
2098 return;
2099 for (raises = TYPE_RAISES_EXCEPTIONS (fntype); raises;
2100 raises = TREE_CHAIN (raises))
2102 tree type = TREE_VALUE (raises);
2103 if (type && !COMPLETE_TYPE_P (type))
2105 if (decl)
2106 error
2107 ("call to function %qD which throws incomplete type %q#T",
2108 decl, type);
2109 else
2110 error ("call to function which throws incomplete type %q#T",
2111 decl);
2117 #include "gt-cp-typeck2.h"