PR preprocessor/63831
[official-gcc.git] / gcc / cp / typeck2.c
blobc53a9b529236e917693c4871778be4c61d9d6e55
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 if ((TREE_SIDE_EFFECTS (init)
608 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
609 || array_of_runtime_bound_p (type))
611 /* For an array, we only need/want a single cleanup region rather
612 than one per element. */
613 tree code = build_vec_init (dest, NULL_TREE, init, false, 1,
614 tf_warning_or_error);
615 add_stmt (code);
616 return true;
618 /* FALLTHRU */
620 case RECORD_TYPE:
621 case UNION_TYPE:
622 case QUAL_UNION_TYPE:
623 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), idx,
624 field_index, value)
626 /* The current implementation of this algorithm assumes that
627 the field was set for all the elements. This is usually done
628 by process_init_constructor. */
629 gcc_assert (field_index);
631 if (!array_type_p)
632 inner_type = TREE_TYPE (field_index);
634 if (TREE_CODE (value) == CONSTRUCTOR)
636 tree sub;
638 if (array_type_p)
639 sub = build4 (ARRAY_REF, inner_type, dest, field_index,
640 NULL_TREE, NULL_TREE);
641 else
642 sub = build3 (COMPONENT_REF, inner_type, dest, field_index,
643 NULL_TREE);
645 if (!split_nonconstant_init_1 (sub, value))
646 complete_p = false;
647 num_split_elts++;
649 else if (!initializer_constant_valid_p (value, inner_type))
651 tree code;
652 tree sub;
654 /* FIXME: Ordered removal is O(1) so the whole function is
655 worst-case quadratic. This could be fixed using an aside
656 bitmap to record which elements must be removed and remove
657 them all at the same time. Or by merging
658 split_non_constant_init into process_init_constructor_array,
659 that is separating constants from non-constants while building
660 the vector. */
661 CONSTRUCTOR_ELTS (init)->ordered_remove (idx);
662 --idx;
664 if (TREE_CODE (field_index) == RANGE_EXPR)
666 /* Use build_vec_init to initialize a range. */
667 tree low = TREE_OPERAND (field_index, 0);
668 tree hi = TREE_OPERAND (field_index, 1);
669 sub = build4 (ARRAY_REF, inner_type, dest, low,
670 NULL_TREE, NULL_TREE);
671 sub = cp_build_addr_expr (sub, tf_warning_or_error);
672 tree max = size_binop (MINUS_EXPR, hi, low);
673 code = build_vec_init (sub, max, value, false, 0,
674 tf_warning_or_error);
675 add_stmt (code);
676 if (tree_fits_shwi_p (max))
677 num_split_elts += tree_to_shwi (max);
679 else
681 if (array_type_p)
682 sub = build4 (ARRAY_REF, inner_type, dest, field_index,
683 NULL_TREE, NULL_TREE);
684 else
685 sub = build3 (COMPONENT_REF, inner_type, dest, field_index,
686 NULL_TREE);
688 code = build2 (INIT_EXPR, inner_type, sub, value);
689 code = build_stmt (input_location, EXPR_STMT, code);
690 code = maybe_cleanup_point_expr_void (code);
691 add_stmt (code);
692 if (type_build_dtor_call (inner_type))
694 code = (build_special_member_call
695 (sub, complete_dtor_identifier, NULL, inner_type,
696 LOOKUP_NORMAL, tf_warning_or_error));
697 if (!TYPE_HAS_TRIVIAL_DESTRUCTOR (inner_type))
698 finish_eh_cleanup (code);
702 num_split_elts++;
705 break;
707 case VECTOR_TYPE:
708 if (!initializer_constant_valid_p (init, type))
710 tree code;
711 tree cons = copy_node (init);
712 CONSTRUCTOR_ELTS (init) = NULL;
713 code = build2 (MODIFY_EXPR, type, dest, cons);
714 code = build_stmt (input_location, EXPR_STMT, code);
715 add_stmt (code);
716 num_split_elts += CONSTRUCTOR_NELTS (init);
718 break;
720 default:
721 gcc_unreachable ();
724 /* The rest of the initializer is now a constant. */
725 TREE_CONSTANT (init) = 1;
726 return complete_p && complete_ctor_at_level_p (TREE_TYPE (init),
727 num_split_elts, inner_type);
730 /* A subroutine of store_init_value. Splits non-constant static
731 initializer INIT into a constant part and generates code to
732 perform the non-constant part of the initialization to DEST.
733 Returns the code for the runtime init. */
735 tree
736 split_nonconstant_init (tree dest, tree init)
738 tree code;
740 if (TREE_CODE (init) == TARGET_EXPR)
741 init = TARGET_EXPR_INITIAL (init);
742 if (TREE_CODE (init) == CONSTRUCTOR)
744 code = push_stmt_list ();
745 if (split_nonconstant_init_1 (dest, init))
746 init = NULL_TREE;
747 code = pop_stmt_list (code);
748 DECL_INITIAL (dest) = init;
749 TREE_READONLY (dest) = 0;
751 else
752 code = build2 (INIT_EXPR, TREE_TYPE (dest), dest, init);
754 return code;
757 /* Perform appropriate conversions on the initial value of a variable,
758 store it in the declaration DECL,
759 and print any error messages that are appropriate.
760 If the init is invalid, store an ERROR_MARK.
762 C++: Note that INIT might be a TREE_LIST, which would mean that it is
763 a base class initializer for some aggregate type, hopefully compatible
764 with DECL. If INIT is a single element, and DECL is an aggregate
765 type, we silently convert INIT into a TREE_LIST, allowing a constructor
766 to be called.
768 If INIT is a TREE_LIST and there is no constructor, turn INIT
769 into a CONSTRUCTOR and use standard initialization techniques.
770 Perhaps a warning should be generated?
772 Returns code to be executed if initialization could not be performed
773 for static variable. In that case, caller must emit the code. */
775 tree
776 store_init_value (tree decl, tree init, vec<tree, va_gc>** cleanups, int flags)
778 tree value, type;
780 /* If variable's type was invalidly declared, just ignore it. */
782 type = TREE_TYPE (decl);
783 if (TREE_CODE (type) == ERROR_MARK)
784 return NULL_TREE;
786 if (MAYBE_CLASS_TYPE_P (type))
788 if (TREE_CODE (init) == TREE_LIST)
790 error ("constructor syntax used, but no constructor declared "
791 "for type %qT", type);
792 init = build_constructor_from_list (init_list_type_node, nreverse (init));
796 /* End of special C++ code. */
798 if (flags & LOOKUP_ALREADY_DIGESTED)
799 value = init;
800 else
801 /* Digest the specified initializer into an expression. */
802 value = digest_init_flags (type, init, flags);
804 value = extend_ref_init_temps (decl, value, cleanups);
806 /* In C++11 constant expression is a semantic, not syntactic, property.
807 In C++98, make sure that what we thought was a constant expression at
808 template definition time is still constant and otherwise perform this
809 as optimization, e.g. to fold SIZEOF_EXPRs in the initializer. */
810 if (decl_maybe_constant_var_p (decl) || TREE_STATIC (decl))
812 bool const_init;
813 value = instantiate_non_dependent_expr (value);
814 if (DECL_DECLARED_CONSTEXPR_P (decl)
815 || DECL_IN_AGGR_P (decl))
817 /* Diagnose a non-constant initializer for constexpr. */
818 if (processing_template_decl
819 && !require_potential_constant_expression (value))
820 value = error_mark_node;
821 else
822 value = cxx_constant_value (value, decl);
824 value = maybe_constant_init (value, decl);
825 if (TREE_CODE (value) == CONSTRUCTOR && cp_has_mutable_p (type))
826 /* Poison this CONSTRUCTOR so it can't be copied to another
827 constexpr variable. */
828 CONSTRUCTOR_MUTABLE_POISON (value) = true;
829 const_init = (reduced_constant_expression_p (value)
830 || error_operand_p (value));
831 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = const_init;
832 TREE_CONSTANT (decl) = const_init && decl_maybe_constant_var_p (decl);
835 if (cxx_dialect >= cxx14)
836 /* Handle aggregate NSDMI in non-constant initializers, too. */
837 value = replace_placeholders (value, decl);
839 /* If the initializer is not a constant, fill in DECL_INITIAL with
840 the bits that are constant, and then return an expression that
841 will perform the dynamic initialization. */
842 if (value != error_mark_node
843 && (TREE_SIDE_EFFECTS (value)
844 || array_of_runtime_bound_p (type)
845 || ! reduced_constant_expression_p (value)))
846 return split_nonconstant_init (decl, value);
847 /* If the value is a constant, just put it in DECL_INITIAL. If DECL
848 is an automatic variable, the middle end will turn this into a
849 dynamic initialization later. */
850 DECL_INITIAL (decl) = value;
851 return NULL_TREE;
855 /* Give diagnostic about narrowing conversions within { }. */
857 bool
858 check_narrowing (tree type, tree init, tsubst_flags_t complain)
860 tree ftype = unlowered_expr_type (init);
861 bool ok = true;
862 REAL_VALUE_TYPE d;
864 if (((!warn_narrowing || !(complain & tf_warning))
865 && cxx_dialect == cxx98)
866 || !ARITHMETIC_TYPE_P (type))
867 return ok;
869 if (BRACE_ENCLOSED_INITIALIZER_P (init)
870 && TREE_CODE (type) == COMPLEX_TYPE)
872 tree elttype = TREE_TYPE (type);
873 if (CONSTRUCTOR_NELTS (init) > 0)
874 ok &= check_narrowing (elttype, CONSTRUCTOR_ELT (init, 0)->value,
875 complain);
876 if (CONSTRUCTOR_NELTS (init) > 1)
877 ok &= check_narrowing (elttype, CONSTRUCTOR_ELT (init, 1)->value,
878 complain);
879 return ok;
882 init = fold_non_dependent_expr (init);
884 if (TREE_CODE (type) == INTEGER_TYPE
885 && TREE_CODE (ftype) == REAL_TYPE)
886 ok = false;
887 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype)
888 && CP_INTEGRAL_TYPE_P (type))
890 if (TREE_CODE (ftype) == ENUMERAL_TYPE)
891 /* Check for narrowing based on the values of the enumeration. */
892 ftype = ENUM_UNDERLYING_TYPE (ftype);
893 if ((tree_int_cst_lt (TYPE_MAX_VALUE (type),
894 TYPE_MAX_VALUE (ftype))
895 || tree_int_cst_lt (TYPE_MIN_VALUE (ftype),
896 TYPE_MIN_VALUE (type)))
897 && (TREE_CODE (init) != INTEGER_CST
898 || !int_fits_type_p (init, type)))
899 ok = false;
901 else if (TREE_CODE (ftype) == REAL_TYPE
902 && TREE_CODE (type) == REAL_TYPE)
904 if (TYPE_PRECISION (type) < TYPE_PRECISION (ftype))
906 if (TREE_CODE (init) == REAL_CST)
908 /* Issue 703: Loss of precision is OK as long as the value is
909 within the representable range of the new type. */
910 REAL_VALUE_TYPE r;
911 d = TREE_REAL_CST (init);
912 real_convert (&r, TYPE_MODE (type), &d);
913 if (real_isinf (&r))
914 ok = false;
916 else
917 ok = false;
920 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype)
921 && TREE_CODE (type) == REAL_TYPE)
923 ok = false;
924 if (TREE_CODE (init) == INTEGER_CST)
926 d = real_value_from_int_cst (0, init);
927 if (exact_real_truncate (TYPE_MODE (type), &d))
928 ok = true;
932 if (!ok)
934 if (cxx_dialect == cxx98)
935 warning_at (EXPR_LOC_OR_LOC (init, input_location), OPT_Wnarrowing,
936 "narrowing conversion of %qE from %qT to %qT inside { } "
937 "is ill-formed in C++11", init, ftype, type);
938 else if (!TREE_CONSTANT (init))
940 if (complain & tf_warning_or_error)
942 pedwarn (EXPR_LOC_OR_LOC (init, input_location), OPT_Wnarrowing,
943 "narrowing conversion of %qE from %qT to %qT inside { }",
944 init, ftype, type);
945 ok = true;
948 else if (complain & tf_error)
949 error_at (EXPR_LOC_OR_LOC (init, input_location),
950 "narrowing conversion of %qE from %qT to %qT inside { }",
951 init, ftype, type);
954 return cxx_dialect == cxx98 || ok;
957 /* Process the initializer INIT for a variable of type TYPE, emitting
958 diagnostics for invalid initializers and converting the initializer as
959 appropriate.
961 For aggregate types, it assumes that reshape_init has already run, thus the
962 initializer will have the right shape (brace elision has been undone).
964 NESTED is true iff we are being called for an element of a CONSTRUCTOR. */
966 static tree
967 digest_init_r (tree type, tree init, bool nested, int flags,
968 tsubst_flags_t complain)
970 enum tree_code code = TREE_CODE (type);
972 if (error_operand_p (init))
973 return error_mark_node;
975 gcc_assert (init);
977 /* We must strip the outermost array type when completing the type,
978 because the its bounds might be incomplete at the moment. */
979 if (!complete_type_or_maybe_complain (TREE_CODE (type) == ARRAY_TYPE
980 ? TREE_TYPE (type) : type, NULL_TREE,
981 complain))
982 return error_mark_node;
984 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue
985 (g++.old-deja/g++.law/casts2.C). */
986 if (TREE_CODE (init) == NON_LVALUE_EXPR)
987 init = TREE_OPERAND (init, 0);
989 /* Initialization of an array of chars from a string constant. The initializer
990 can be optionally enclosed in braces, but reshape_init has already removed
991 them if they were present. */
992 if (code == ARRAY_TYPE)
994 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
995 if (char_type_p (typ1)
996 /*&& init */
997 && TREE_CODE (init) == STRING_CST)
999 tree char_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (init)));
1001 if (TYPE_PRECISION (typ1) == BITS_PER_UNIT)
1003 if (char_type != char_type_node)
1005 if (complain & tf_error)
1006 error ("char-array initialized from wide string");
1007 return error_mark_node;
1010 else
1012 if (char_type == char_type_node)
1014 if (complain & tf_error)
1015 error ("int-array initialized from non-wide string");
1016 return error_mark_node;
1018 else if (char_type != typ1)
1020 if (complain & tf_error)
1021 error ("int-array initialized from incompatible "
1022 "wide string");
1023 return error_mark_node;
1027 if (type != TREE_TYPE (init))
1029 init = copy_node (init);
1030 TREE_TYPE (init) = type;
1032 if (TYPE_DOMAIN (type) != 0 && TREE_CONSTANT (TYPE_SIZE (type)))
1034 int size = TREE_INT_CST_LOW (TYPE_SIZE (type));
1035 size = (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
1036 /* In C it is ok to subtract 1 from the length of the string
1037 because it's ok to ignore the terminating null char that is
1038 counted in the length of the constant, but in C++ this would
1039 be invalid. */
1040 if (size < TREE_STRING_LENGTH (init))
1041 permerror (input_location, "initializer-string for array "
1042 "of chars is too long");
1044 return init;
1048 /* Handle scalar types (including conversions) and references. */
1049 if ((TREE_CODE (type) != COMPLEX_TYPE
1050 || BRACE_ENCLOSED_INITIALIZER_P (init))
1051 && (SCALAR_TYPE_P (type) || code == REFERENCE_TYPE))
1053 tree *exp;
1055 if (nested)
1056 flags |= LOOKUP_NO_NARROWING;
1057 init = convert_for_initialization (0, type, init, flags,
1058 ICR_INIT, NULL_TREE, 0,
1059 complain);
1060 exp = &init;
1062 /* Skip any conversions since we'll be outputting the underlying
1063 constant. */
1064 while (CONVERT_EXPR_P (*exp)
1065 || TREE_CODE (*exp) == NON_LVALUE_EXPR)
1066 exp = &TREE_OPERAND (*exp, 0);
1068 *exp = cplus_expand_constant (*exp);
1070 return init;
1073 /* Come here only for aggregates: records, arrays, unions, complex numbers
1074 and vectors. */
1075 gcc_assert (TREE_CODE (type) == ARRAY_TYPE
1076 || TREE_CODE (type) == VECTOR_TYPE
1077 || TREE_CODE (type) == RECORD_TYPE
1078 || TREE_CODE (type) == UNION_TYPE
1079 || TREE_CODE (type) == COMPLEX_TYPE);
1081 if (BRACE_ENCLOSED_INITIALIZER_P (init)
1082 && !TYPE_NON_AGGREGATE_CLASS (type))
1083 return process_init_constructor (type, init, complain);
1084 else
1086 if (COMPOUND_LITERAL_P (init) && TREE_CODE (type) == ARRAY_TYPE)
1088 if (complain & tf_error)
1089 error ("cannot initialize aggregate of type %qT with "
1090 "a compound literal", type);
1092 return error_mark_node;
1095 if (TREE_CODE (type) == ARRAY_TYPE
1096 && !BRACE_ENCLOSED_INITIALIZER_P (init))
1098 /* Allow the result of build_array_copy and of
1099 build_value_init_noctor. */
1100 if ((TREE_CODE (init) == VEC_INIT_EXPR
1101 || TREE_CODE (init) == CONSTRUCTOR)
1102 && (same_type_ignoring_top_level_qualifiers_p
1103 (type, TREE_TYPE (init))))
1104 return init;
1106 if (complain & tf_error)
1107 error ("array must be initialized with a brace-enclosed"
1108 " initializer");
1109 return error_mark_node;
1112 return convert_for_initialization (NULL_TREE, type, init,
1113 flags,
1114 ICR_INIT, NULL_TREE, 0,
1115 complain);
1119 tree
1120 digest_init (tree type, tree init, tsubst_flags_t complain)
1122 return digest_init_r (type, init, false, LOOKUP_IMPLICIT, complain);
1125 tree
1126 digest_init_flags (tree type, tree init, int flags)
1128 return digest_init_r (type, init, false, flags, tf_warning_or_error);
1131 /* Process the initializer INIT for an NSDMI DECL (a FIELD_DECL). */
1132 tree
1133 digest_nsdmi_init (tree decl, tree init)
1135 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
1137 int flags = LOOKUP_IMPLICIT;
1138 if (DIRECT_LIST_INIT_P (init))
1139 flags = LOOKUP_NORMAL;
1140 init = digest_init_flags (TREE_TYPE (decl), init, flags);
1141 if (TREE_CODE (init) == TARGET_EXPR)
1142 /* This represents the whole initialization. */
1143 TARGET_EXPR_DIRECT_INIT_P (init) = true;
1144 return init;
1147 /* Set of flags used within process_init_constructor to describe the
1148 initializers. */
1149 #define PICFLAG_ERRONEOUS 1
1150 #define PICFLAG_NOT_ALL_CONSTANT 2
1151 #define PICFLAG_NOT_ALL_SIMPLE 4
1152 #define PICFLAG_SIDE_EFFECTS 8
1154 /* Given an initializer INIT, return the flag (PICFLAG_*) which better
1155 describe it. */
1157 static int
1158 picflag_from_initializer (tree init)
1160 if (init == error_mark_node)
1161 return PICFLAG_ERRONEOUS;
1162 else if (!TREE_CONSTANT (init))
1164 if (TREE_SIDE_EFFECTS (init))
1165 return PICFLAG_SIDE_EFFECTS;
1166 else
1167 return PICFLAG_NOT_ALL_CONSTANT;
1169 else if (!initializer_constant_valid_p (init, TREE_TYPE (init)))
1170 return PICFLAG_NOT_ALL_SIMPLE;
1171 return 0;
1174 /* Adjust INIT for going into a CONSTRUCTOR. */
1176 static tree
1177 massage_init_elt (tree type, tree init, tsubst_flags_t complain)
1179 init = digest_init_r (type, init, true, LOOKUP_IMPLICIT, complain);
1180 /* Strip a simple TARGET_EXPR when we know this is an initializer. */
1181 if (TREE_CODE (init) == TARGET_EXPR
1182 && !VOID_TYPE_P (TREE_TYPE (TARGET_EXPR_INITIAL (init))))
1183 init = TARGET_EXPR_INITIAL (init);
1184 /* When we defer constant folding within a statement, we may want to
1185 defer this folding as well. */
1186 tree t = fold_non_dependent_expr (init);
1187 t = maybe_constant_init (t);
1188 if (TREE_CONSTANT (t))
1189 init = t;
1190 return init;
1193 /* Subroutine of process_init_constructor, which will process an initializer
1194 INIT for an array or vector of type TYPE. Returns the flags (PICFLAG_*)
1195 which describe the initializers. */
1197 static int
1198 process_init_constructor_array (tree type, tree init,
1199 tsubst_flags_t complain)
1201 unsigned HOST_WIDE_INT i, len = 0;
1202 int flags = 0;
1203 bool unbounded = false;
1204 constructor_elt *ce;
1205 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (init);
1207 gcc_assert (TREE_CODE (type) == ARRAY_TYPE
1208 || TREE_CODE (type) == VECTOR_TYPE);
1210 if (TREE_CODE (type) == ARRAY_TYPE)
1212 tree domain = TYPE_DOMAIN (type);
1213 if (domain && TREE_CONSTANT (TYPE_MAX_VALUE (domain)))
1214 len = wi::ext (wi::to_offset (TYPE_MAX_VALUE (domain))
1215 - wi::to_offset (TYPE_MIN_VALUE (domain)) + 1,
1216 TYPE_PRECISION (TREE_TYPE (domain)),
1217 TYPE_SIGN (TREE_TYPE (domain))).to_uhwi ();
1218 else
1219 unbounded = true; /* Take as many as there are. */
1221 else
1222 /* Vectors are like simple fixed-size arrays. */
1223 len = TYPE_VECTOR_SUBPARTS (type);
1225 /* There must not be more initializers than needed. */
1226 if (!unbounded && vec_safe_length (v) > len)
1228 if (complain & tf_error)
1229 error ("too many initializers for %qT", type);
1230 else
1231 return PICFLAG_ERRONEOUS;
1234 FOR_EACH_VEC_SAFE_ELT (v, i, ce)
1236 if (ce->index)
1238 gcc_assert (TREE_CODE (ce->index) == INTEGER_CST);
1239 if (compare_tree_int (ce->index, i) != 0)
1241 ce->value = error_mark_node;
1242 sorry ("non-trivial designated initializers not supported");
1245 else
1246 ce->index = size_int (i);
1247 gcc_assert (ce->value);
1248 ce->value = massage_init_elt (TREE_TYPE (type), ce->value, complain);
1250 if (ce->value != error_mark_node)
1251 gcc_assert (same_type_ignoring_top_level_qualifiers_p
1252 (TREE_TYPE (type), TREE_TYPE (ce->value)));
1254 flags |= picflag_from_initializer (ce->value);
1257 /* No more initializers. If the array is unbounded, we are done. Otherwise,
1258 we must add initializers ourselves. */
1259 if (!unbounded)
1260 for (; i < len; ++i)
1262 tree next;
1264 if (type_build_ctor_call (TREE_TYPE (type)))
1266 /* If this type needs constructors run for default-initialization,
1267 we can't rely on the back end to do it for us, so make the
1268 initialization explicit by list-initializing from T{}. */
1269 next = build_constructor (init_list_type_node, NULL);
1270 CONSTRUCTOR_IS_DIRECT_INIT (next) = true;
1271 next = massage_init_elt (TREE_TYPE (type), next, complain);
1272 if (initializer_zerop (next))
1273 /* The default zero-initialization is fine for us; don't
1274 add anything to the CONSTRUCTOR. */
1275 next = NULL_TREE;
1277 else if (!zero_init_p (TREE_TYPE (type)))
1278 next = build_zero_init (TREE_TYPE (type),
1279 /*nelts=*/NULL_TREE,
1280 /*static_storage_p=*/false);
1281 else
1282 /* The default zero-initialization is fine for us; don't
1283 add anything to the CONSTRUCTOR. */
1284 next = NULL_TREE;
1286 if (next)
1288 flags |= picflag_from_initializer (next);
1289 CONSTRUCTOR_APPEND_ELT (v, size_int (i), next);
1293 CONSTRUCTOR_ELTS (init) = v;
1294 return flags;
1297 /* Subroutine of process_init_constructor, which will process an initializer
1298 INIT for a class of type TYPE. Returns the flags (PICFLAG_*) which describe
1299 the initializers. */
1301 static int
1302 process_init_constructor_record (tree type, tree init,
1303 tsubst_flags_t complain)
1305 vec<constructor_elt, va_gc> *v = NULL;
1306 tree field;
1307 int skipped = 0;
1309 gcc_assert (TREE_CODE (type) == RECORD_TYPE);
1310 gcc_assert (!CLASSTYPE_VBASECLASSES (type));
1311 gcc_assert (!TYPE_BINFO (type)
1312 || !BINFO_N_BASE_BINFOS (TYPE_BINFO (type)));
1313 gcc_assert (!TYPE_POLYMORPHIC_P (type));
1315 restart:
1316 int flags = 0;
1317 unsigned HOST_WIDE_INT idx = 0;
1318 /* Generally, we will always have an index for each initializer (which is
1319 a FIELD_DECL, put by reshape_init), but compound literals don't go trough
1320 reshape_init. So we need to handle both cases. */
1321 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
1323 tree next;
1324 tree type;
1326 if (!DECL_NAME (field) && DECL_C_BIT_FIELD (field))
1327 continue;
1329 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
1330 continue;
1332 /* If this is a bitfield, first convert to the declared type. */
1333 type = TREE_TYPE (field);
1334 if (DECL_BIT_FIELD_TYPE (field))
1335 type = DECL_BIT_FIELD_TYPE (field);
1336 if (type == error_mark_node)
1337 return PICFLAG_ERRONEOUS;
1339 if (idx < vec_safe_length (CONSTRUCTOR_ELTS (init)))
1341 constructor_elt *ce = &(*CONSTRUCTOR_ELTS (init))[idx];
1342 if (ce->index)
1344 /* We can have either a FIELD_DECL or an IDENTIFIER_NODE. The
1345 latter case can happen in templates where lookup has to be
1346 deferred. */
1347 gcc_assert (TREE_CODE (ce->index) == FIELD_DECL
1348 || identifier_p (ce->index));
1349 if (ce->index != field
1350 && ce->index != DECL_NAME (field))
1352 ce->value = error_mark_node;
1353 sorry ("non-trivial designated initializers not supported");
1357 gcc_assert (ce->value);
1358 next = massage_init_elt (type, ce->value, complain);
1359 ++idx;
1361 else if (DECL_INITIAL (field))
1363 if (skipped > 0)
1365 /* We're using an NSDMI past a field with implicit
1366 zero-init. Go back and make it explicit. */
1367 skipped = -1;
1368 vec_safe_truncate (v, 0);
1369 goto restart;
1371 /* C++14 aggregate NSDMI. */
1372 next = get_nsdmi (field, /*ctor*/false);
1374 else if (type_build_ctor_call (TREE_TYPE (field)))
1376 /* If this type needs constructors run for
1377 default-initialization, we can't rely on the back end to do it
1378 for us, so build up TARGET_EXPRs. If the type in question is
1379 a class, just build one up; if it's an array, recurse. */
1380 next = build_constructor (init_list_type_node, NULL);
1381 /* Call this direct-initialization pending DR 1518 resolution so
1382 that explicit default ctors don't break valid C++03 code. */
1383 CONSTRUCTOR_IS_DIRECT_INIT (next) = true;
1384 next = massage_init_elt (TREE_TYPE (field), next, complain);
1386 /* Warn when some struct elements are implicitly initialized. */
1387 if ((complain & tf_warning)
1388 && !EMPTY_CONSTRUCTOR_P (init))
1389 warning (OPT_Wmissing_field_initializers,
1390 "missing initializer for member %qD", field);
1392 else
1394 if (TREE_CODE (TREE_TYPE (field)) == REFERENCE_TYPE)
1396 if (complain & tf_error)
1397 error ("member %qD is uninitialized reference", field);
1398 else
1399 return PICFLAG_ERRONEOUS;
1401 else if (CLASSTYPE_REF_FIELDS_NEED_INIT (TREE_TYPE (field)))
1403 if (complain & tf_error)
1404 error ("member %qD with uninitialized reference fields", field);
1405 else
1406 return PICFLAG_ERRONEOUS;
1409 /* Warn when some struct elements are implicitly initialized
1410 to zero. */
1411 if ((complain & tf_warning)
1412 && !EMPTY_CONSTRUCTOR_P (init))
1413 warning (OPT_Wmissing_field_initializers,
1414 "missing initializer for member %qD", field);
1416 if (!zero_init_p (TREE_TYPE (field))
1417 || skipped < 0)
1418 next = build_zero_init (TREE_TYPE (field), /*nelts=*/NULL_TREE,
1419 /*static_storage_p=*/false);
1420 else
1422 /* The default zero-initialization is fine for us; don't
1423 add anything to the CONSTRUCTOR. */
1424 skipped = 1;
1425 continue;
1429 /* If this is a bitfield, now convert to the lowered type. */
1430 if (type != TREE_TYPE (field))
1431 next = cp_convert_and_check (TREE_TYPE (field), next, complain);
1432 flags |= picflag_from_initializer (next);
1433 CONSTRUCTOR_APPEND_ELT (v, field, next);
1436 if (idx < vec_safe_length (CONSTRUCTOR_ELTS (init)))
1438 if (complain & tf_error)
1439 error ("too many initializers for %qT", type);
1440 else
1441 return PICFLAG_ERRONEOUS;
1444 CONSTRUCTOR_ELTS (init) = v;
1445 return flags;
1448 /* Subroutine of process_init_constructor, which will process a single
1449 initializer INIT for a union of type TYPE. Returns the flags (PICFLAG_*)
1450 which describe the initializer. */
1452 static int
1453 process_init_constructor_union (tree type, tree init,
1454 tsubst_flags_t complain)
1456 constructor_elt *ce;
1457 int len;
1459 /* If the initializer was empty, use default zero initialization. */
1460 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
1461 return 0;
1463 len = CONSTRUCTOR_ELTS (init)->length ();
1464 if (len > 1)
1466 if (!(complain & tf_error))
1467 return PICFLAG_ERRONEOUS;
1468 error ("too many initializers for %qT", type);
1469 CONSTRUCTOR_ELTS (init)->block_remove (1, len-1);
1472 ce = &(*CONSTRUCTOR_ELTS (init))[0];
1474 /* If this element specifies a field, initialize via that field. */
1475 if (ce->index)
1477 if (TREE_CODE (ce->index) == FIELD_DECL)
1479 else if (identifier_p (ce->index))
1481 /* This can happen within a cast, see g++.dg/opt/cse2.C. */
1482 tree name = ce->index;
1483 tree field;
1484 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1485 if (DECL_NAME (field) == name)
1486 break;
1487 if (!field)
1489 if (complain & tf_error)
1490 error ("no field %qD found in union being initialized",
1491 field);
1492 ce->value = error_mark_node;
1494 ce->index = field;
1496 else
1498 gcc_assert (TREE_CODE (ce->index) == INTEGER_CST
1499 || TREE_CODE (ce->index) == RANGE_EXPR);
1500 if (complain & tf_error)
1501 error ("index value instead of field name in union initializer");
1502 ce->value = error_mark_node;
1505 else
1507 /* Find the first named field. ANSI decided in September 1990
1508 that only named fields count here. */
1509 tree field = TYPE_FIELDS (type);
1510 while (field && (!DECL_NAME (field) || TREE_CODE (field) != FIELD_DECL))
1511 field = TREE_CHAIN (field);
1512 if (field == NULL_TREE)
1514 if (complain & tf_error)
1515 error ("too many initializers for %qT", type);
1516 ce->value = error_mark_node;
1518 ce->index = field;
1521 if (ce->value && ce->value != error_mark_node)
1522 ce->value = massage_init_elt (TREE_TYPE (ce->index), ce->value, complain);
1524 return picflag_from_initializer (ce->value);
1527 /* Process INIT, a constructor for a variable of aggregate type TYPE. The
1528 constructor is a brace-enclosed initializer, and will be modified in-place.
1530 Each element is converted to the right type through digest_init, and
1531 missing initializers are added following the language rules (zero-padding,
1532 etc.).
1534 After the execution, the initializer will have TREE_CONSTANT if all elts are
1535 constant, and TREE_STATIC set if, in addition, all elts are simple enough
1536 constants that the assembler and linker can compute them.
1538 The function returns the initializer itself, or error_mark_node in case
1539 of error. */
1541 static tree
1542 process_init_constructor (tree type, tree init, tsubst_flags_t complain)
1544 int flags;
1546 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
1548 if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == VECTOR_TYPE)
1549 flags = process_init_constructor_array (type, init, complain);
1550 else if (TREE_CODE (type) == RECORD_TYPE)
1551 flags = process_init_constructor_record (type, init, complain);
1552 else if (TREE_CODE (type) == UNION_TYPE)
1553 flags = process_init_constructor_union (type, init, complain);
1554 else
1555 gcc_unreachable ();
1557 if (flags & PICFLAG_ERRONEOUS)
1558 return error_mark_node;
1560 TREE_TYPE (init) = type;
1561 if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == NULL_TREE)
1562 cp_complete_array_type (&TREE_TYPE (init), init, /*do_default=*/0);
1563 if (flags & PICFLAG_SIDE_EFFECTS)
1565 TREE_CONSTANT (init) = false;
1566 TREE_SIDE_EFFECTS (init) = true;
1568 else if (flags & PICFLAG_NOT_ALL_CONSTANT)
1569 /* Make sure TREE_CONSTANT isn't set from build_constructor. */
1570 TREE_CONSTANT (init) = false;
1571 else
1573 TREE_CONSTANT (init) = 1;
1574 if (!(flags & PICFLAG_NOT_ALL_SIMPLE))
1575 TREE_STATIC (init) = 1;
1577 return init;
1580 /* Given a structure or union value DATUM, construct and return
1581 the structure or union component which results from narrowing
1582 that value to the base specified in BASETYPE. For example, given the
1583 hierarchy
1585 class L { int ii; };
1586 class A : L { ... };
1587 class B : L { ... };
1588 class C : A, B { ... };
1590 and the declaration
1592 C x;
1594 then the expression
1596 x.A::ii refers to the ii member of the L part of
1597 the A part of the C object named by X. In this case,
1598 DATUM would be x, and BASETYPE would be A.
1600 I used to think that this was nonconformant, that the standard specified
1601 that first we look up ii in A, then convert x to an L& and pull out the
1602 ii part. But in fact, it does say that we convert x to an A&; A here
1603 is known as the "naming class". (jason 2000-12-19)
1605 BINFO_P points to a variable initialized either to NULL_TREE or to the
1606 binfo for the specific base subobject we want to convert to. */
1608 tree
1609 build_scoped_ref (tree datum, tree basetype, tree* binfo_p)
1611 tree binfo;
1613 if (datum == error_mark_node)
1614 return error_mark_node;
1615 if (*binfo_p)
1616 binfo = *binfo_p;
1617 else
1618 binfo = lookup_base (TREE_TYPE (datum), basetype, ba_check,
1619 NULL, tf_warning_or_error);
1621 if (!binfo || binfo == error_mark_node)
1623 *binfo_p = NULL_TREE;
1624 if (!binfo)
1625 error_not_base_type (basetype, TREE_TYPE (datum));
1626 return error_mark_node;
1629 *binfo_p = binfo;
1630 return build_base_path (PLUS_EXPR, datum, binfo, 1,
1631 tf_warning_or_error);
1634 /* Build a reference to an object specified by the C++ `->' operator.
1635 Usually this just involves dereferencing the object, but if the
1636 `->' operator is overloaded, then such overloads must be
1637 performed until an object which does not have the `->' operator
1638 overloaded is found. An error is reported when circular pointer
1639 delegation is detected. */
1641 tree
1642 build_x_arrow (location_t loc, tree expr, tsubst_flags_t complain)
1644 tree orig_expr = expr;
1645 tree type = TREE_TYPE (expr);
1646 tree last_rval = NULL_TREE;
1647 vec<tree, va_gc> *types_memoized = NULL;
1649 if (type == error_mark_node)
1650 return error_mark_node;
1652 if (processing_template_decl)
1654 if (type_dependent_expression_p (expr))
1655 return build_min_nt_loc (loc, ARROW_EXPR, expr);
1656 expr = build_non_dependent_expr (expr);
1659 if (MAYBE_CLASS_TYPE_P (type))
1661 struct tinst_level *actual_inst = current_instantiation ();
1662 tree fn = NULL;
1664 while ((expr = build_new_op (loc, COMPONENT_REF,
1665 LOOKUP_NORMAL, expr, NULL_TREE, NULL_TREE,
1666 &fn, complain)))
1668 if (expr == error_mark_node)
1669 return error_mark_node;
1671 /* This provides a better instantiation backtrace in case of
1672 error. */
1673 if (fn && DECL_USE_TEMPLATE (fn))
1674 push_tinst_level_loc (fn,
1675 (current_instantiation () != actual_inst)
1676 ? DECL_SOURCE_LOCATION (fn)
1677 : input_location);
1678 fn = NULL;
1680 if (vec_member (TREE_TYPE (expr), types_memoized))
1682 if (complain & tf_error)
1683 error ("circular pointer delegation detected");
1684 return error_mark_node;
1687 vec_safe_push (types_memoized, TREE_TYPE (expr));
1688 last_rval = expr;
1691 while (current_instantiation () != actual_inst)
1692 pop_tinst_level ();
1694 if (last_rval == NULL_TREE)
1696 if (complain & tf_error)
1697 error ("base operand of %<->%> has non-pointer type %qT", type);
1698 return error_mark_node;
1701 if (TREE_CODE (TREE_TYPE (last_rval)) == REFERENCE_TYPE)
1702 last_rval = convert_from_reference (last_rval);
1704 else
1705 last_rval = decay_conversion (expr, complain);
1707 if (TYPE_PTR_P (TREE_TYPE (last_rval)))
1709 if (processing_template_decl)
1711 expr = build_min (ARROW_EXPR, TREE_TYPE (TREE_TYPE (last_rval)),
1712 orig_expr);
1713 TREE_SIDE_EFFECTS (expr) = TREE_SIDE_EFFECTS (last_rval);
1714 return expr;
1717 return cp_build_indirect_ref (last_rval, RO_NULL, complain);
1720 if (complain & tf_error)
1722 if (types_memoized)
1723 error ("result of %<operator->()%> yields non-pointer result");
1724 else
1725 error ("base operand of %<->%> is not a pointer");
1727 return error_mark_node;
1730 /* Return an expression for "DATUM .* COMPONENT". DATUM has not
1731 already been checked out to be of aggregate type. */
1733 tree
1734 build_m_component_ref (tree datum, tree component, tsubst_flags_t complain)
1736 tree ptrmem_type;
1737 tree objtype;
1738 tree type;
1739 tree binfo;
1740 tree ctype;
1742 if (error_operand_p (datum) || error_operand_p (component))
1743 return error_mark_node;
1745 datum = mark_lvalue_use (datum);
1746 component = mark_rvalue_use (component);
1748 ptrmem_type = TREE_TYPE (component);
1749 if (!TYPE_PTRMEM_P (ptrmem_type))
1751 if (complain & tf_error)
1752 error ("%qE cannot be used as a member pointer, since it is of "
1753 "type %qT", component, ptrmem_type);
1754 return error_mark_node;
1757 objtype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
1758 if (! MAYBE_CLASS_TYPE_P (objtype))
1760 if (complain & tf_error)
1761 error ("cannot apply member pointer %qE to %qE, which is of "
1762 "non-class type %qT", component, datum, objtype);
1763 return error_mark_node;
1766 type = TYPE_PTRMEM_POINTED_TO_TYPE (ptrmem_type);
1767 ctype = complete_type (TYPE_PTRMEM_CLASS_TYPE (ptrmem_type));
1769 if (!COMPLETE_TYPE_P (ctype))
1771 if (!same_type_p (ctype, objtype))
1772 goto mismatch;
1773 binfo = NULL;
1775 else
1777 binfo = lookup_base (objtype, ctype, ba_check, NULL, complain);
1779 if (!binfo)
1781 mismatch:
1782 if (complain & tf_error)
1783 error ("pointer to member type %qT incompatible with object "
1784 "type %qT", type, objtype);
1785 return error_mark_node;
1787 else if (binfo == error_mark_node)
1788 return error_mark_node;
1791 if (TYPE_PTRDATAMEM_P (ptrmem_type))
1793 cp_lvalue_kind kind = lvalue_kind (datum);
1794 tree ptype;
1796 /* Compute the type of the field, as described in [expr.ref].
1797 There's no such thing as a mutable pointer-to-member, so
1798 things are not as complex as they are for references to
1799 non-static data members. */
1800 type = cp_build_qualified_type (type,
1801 (cp_type_quals (type)
1802 | cp_type_quals (TREE_TYPE (datum))));
1804 datum = build_address (datum);
1806 /* Convert object to the correct base. */
1807 if (binfo)
1809 datum = build_base_path (PLUS_EXPR, datum, binfo, 1, complain);
1810 if (datum == error_mark_node)
1811 return error_mark_node;
1814 /* Build an expression for "object + offset" where offset is the
1815 value stored in the pointer-to-data-member. */
1816 ptype = build_pointer_type (type);
1817 datum = fold_build_pointer_plus (fold_convert (ptype, datum), component);
1818 datum = cp_build_indirect_ref (datum, RO_NULL, complain);
1819 if (datum == error_mark_node)
1820 return error_mark_node;
1822 /* If the object expression was an rvalue, return an rvalue. */
1823 if (kind & clk_class)
1824 datum = rvalue (datum);
1825 else if (kind & clk_rvalueref)
1826 datum = move (datum);
1827 return datum;
1829 else
1831 /* 5.5/6: In a .* expression whose object expression is an rvalue, the
1832 program is ill-formed if the second operand is a pointer to member
1833 function with ref-qualifier &. In a .* expression whose object
1834 expression is an lvalue, the program is ill-formed if the second
1835 operand is a pointer to member function with ref-qualifier &&. */
1836 if (FUNCTION_REF_QUALIFIED (type))
1838 bool lval = real_lvalue_p (datum);
1839 if (lval && FUNCTION_RVALUE_QUALIFIED (type))
1841 if (complain & tf_error)
1842 error ("pointer-to-member-function type %qT requires an rvalue",
1843 ptrmem_type);
1844 return error_mark_node;
1846 else if (!lval && !FUNCTION_RVALUE_QUALIFIED (type))
1848 if (complain & tf_error)
1849 error ("pointer-to-member-function type %qT requires an lvalue",
1850 ptrmem_type);
1851 return error_mark_node;
1854 return build2 (OFFSET_REF, type, datum, component);
1858 /* Return a tree node for the expression TYPENAME '(' PARMS ')'. */
1860 tree
1861 build_functional_cast (tree exp, tree parms, tsubst_flags_t complain)
1863 /* This is either a call to a constructor,
1864 or a C cast in C++'s `functional' notation. */
1866 /* The type to which we are casting. */
1867 tree type;
1868 vec<tree, va_gc> *parmvec;
1870 if (error_operand_p (exp) || parms == error_mark_node)
1871 return error_mark_node;
1873 if (TREE_CODE (exp) == TYPE_DECL)
1875 type = TREE_TYPE (exp);
1877 if (complain & tf_warning
1878 && TREE_DEPRECATED (type)
1879 && DECL_ARTIFICIAL (exp))
1880 warn_deprecated_use (type, NULL_TREE);
1882 else
1883 type = exp;
1885 /* We need to check this explicitly, since value-initialization of
1886 arrays is allowed in other situations. */
1887 if (TREE_CODE (type) == ARRAY_TYPE)
1889 if (complain & tf_error)
1890 error ("functional cast to array type %qT", type);
1891 return error_mark_node;
1894 if (type_uses_auto (type))
1896 if (complain & tf_error)
1897 error ("invalid use of %<auto%>");
1898 return error_mark_node;
1901 if (processing_template_decl)
1903 tree t;
1905 /* Diagnose this even in a template. We could also try harder
1906 to give all the usual errors when the type and args are
1907 non-dependent... */
1908 if (TREE_CODE (type) == REFERENCE_TYPE && !parms)
1910 if (complain & tf_error)
1911 error ("invalid value-initialization of reference type");
1912 return error_mark_node;
1915 t = build_min (CAST_EXPR, type, parms);
1916 /* We don't know if it will or will not have side effects. */
1917 TREE_SIDE_EFFECTS (t) = 1;
1918 return t;
1921 if (! MAYBE_CLASS_TYPE_P (type))
1923 if (parms == NULL_TREE)
1925 if (VOID_TYPE_P (type))
1926 return void_node;
1927 return build_value_init (cv_unqualified (type), complain);
1930 /* This must build a C cast. */
1931 parms = build_x_compound_expr_from_list (parms, ELK_FUNC_CAST, complain);
1932 return cp_build_c_cast (type, parms, complain);
1935 /* Prepare to evaluate as a call to a constructor. If this expression
1936 is actually used, for example,
1938 return X (arg1, arg2, ...);
1940 then the slot being initialized will be filled in. */
1942 if (!complete_type_or_maybe_complain (type, NULL_TREE, complain))
1943 return error_mark_node;
1944 if (abstract_virtuals_error_sfinae (ACU_CAST, type, complain))
1945 return error_mark_node;
1947 /* [expr.type.conv]
1949 If the expression list is a single-expression, the type
1950 conversion is equivalent (in definedness, and if defined in
1951 meaning) to the corresponding cast expression. */
1952 if (parms && TREE_CHAIN (parms) == NULL_TREE)
1953 return cp_build_c_cast (type, TREE_VALUE (parms), complain);
1955 /* [expr.type.conv]
1957 The expression T(), where T is a simple-type-specifier for a
1958 non-array complete object type or the (possibly cv-qualified)
1959 void type, creates an rvalue of the specified type, which is
1960 value-initialized. */
1962 if (parms == NULL_TREE)
1964 exp = build_value_init (type, complain);
1965 exp = get_target_expr_sfinae (exp, complain);
1966 return exp;
1969 /* Call the constructor. */
1970 parmvec = make_tree_vector ();
1971 for (; parms != NULL_TREE; parms = TREE_CHAIN (parms))
1972 vec_safe_push (parmvec, TREE_VALUE (parms));
1973 exp = build_special_member_call (NULL_TREE, complete_ctor_identifier,
1974 &parmvec, type, LOOKUP_NORMAL, complain);
1975 release_tree_vector (parmvec);
1977 if (exp == error_mark_node)
1978 return error_mark_node;
1980 return build_cplus_new (type, exp, complain);
1984 /* Add new exception specifier SPEC, to the LIST we currently have.
1985 If it's already in LIST then do nothing.
1986 Moan if it's bad and we're allowed to. COMPLAIN < 0 means we
1987 know what we're doing. */
1989 tree
1990 add_exception_specifier (tree list, tree spec, int complain)
1992 bool ok;
1993 tree core = spec;
1994 bool is_ptr;
1995 diagnostic_t diag_type = DK_UNSPECIFIED; /* none */
1997 if (spec == error_mark_node)
1998 return list;
2000 gcc_assert (spec && (!list || TREE_VALUE (list)));
2002 /* [except.spec] 1, type in an exception specifier shall not be
2003 incomplete, or pointer or ref to incomplete other than pointer
2004 to cv void. */
2005 is_ptr = TYPE_PTR_P (core);
2006 if (is_ptr || TREE_CODE (core) == REFERENCE_TYPE)
2007 core = TREE_TYPE (core);
2008 if (complain < 0)
2009 ok = true;
2010 else if (VOID_TYPE_P (core))
2011 ok = is_ptr;
2012 else if (TREE_CODE (core) == TEMPLATE_TYPE_PARM)
2013 ok = true;
2014 else if (processing_template_decl)
2015 ok = true;
2016 else
2018 ok = true;
2019 /* 15.4/1 says that types in an exception specifier must be complete,
2020 but it seems more reasonable to only require this on definitions
2021 and calls. So just give a pedwarn at this point; we will give an
2022 error later if we hit one of those two cases. */
2023 if (!COMPLETE_TYPE_P (complete_type (core)))
2024 diag_type = DK_PEDWARN; /* pedwarn */
2027 if (ok)
2029 tree probe;
2031 for (probe = list; probe; probe = TREE_CHAIN (probe))
2032 if (same_type_p (TREE_VALUE (probe), spec))
2033 break;
2034 if (!probe)
2035 list = tree_cons (NULL_TREE, spec, list);
2037 else
2038 diag_type = DK_ERROR; /* error */
2040 if (diag_type != DK_UNSPECIFIED
2041 && (complain & tf_warning_or_error))
2042 cxx_incomplete_type_diagnostic (NULL_TREE, core, diag_type);
2044 return list;
2047 /* Like nothrow_spec_p, but don't abort on deferred noexcept. */
2049 static bool
2050 nothrow_spec_p_uninst (const_tree spec)
2052 if (DEFERRED_NOEXCEPT_SPEC_P (spec))
2053 return false;
2054 return nothrow_spec_p (spec);
2057 /* Combine the two exceptions specifier lists LIST and ADD, and return
2058 their union. */
2060 tree
2061 merge_exception_specifiers (tree list, tree add)
2063 tree noex, orig_list;
2065 /* No exception-specifier or noexcept(false) are less strict than
2066 anything else. Prefer the newer variant (LIST). */
2067 if (!list || list == noexcept_false_spec)
2068 return list;
2069 else if (!add || add == noexcept_false_spec)
2070 return add;
2072 /* noexcept(true) and throw() are stricter than anything else.
2073 As above, prefer the more recent one (LIST). */
2074 if (nothrow_spec_p_uninst (add))
2075 return list;
2077 /* Two implicit noexcept specs (e.g. on a destructor) are equivalent. */
2078 if (UNEVALUATED_NOEXCEPT_SPEC_P (add)
2079 && UNEVALUATED_NOEXCEPT_SPEC_P (list))
2080 return list;
2081 /* We should have instantiated other deferred noexcept specs by now. */
2082 gcc_assert (!DEFERRED_NOEXCEPT_SPEC_P (add));
2084 if (nothrow_spec_p_uninst (list))
2085 return add;
2086 noex = TREE_PURPOSE (list);
2087 gcc_checking_assert (!TREE_PURPOSE (add)
2088 || cp_tree_equal (noex, TREE_PURPOSE (add)));
2090 /* Combine the dynamic-exception-specifiers, if any. */
2091 orig_list = list;
2092 for (; add && TREE_VALUE (add); add = TREE_CHAIN (add))
2094 tree spec = TREE_VALUE (add);
2095 tree probe;
2097 for (probe = orig_list; probe && TREE_VALUE (probe);
2098 probe = TREE_CHAIN (probe))
2099 if (same_type_p (TREE_VALUE (probe), spec))
2100 break;
2101 if (!probe)
2103 spec = build_tree_list (NULL_TREE, spec);
2104 TREE_CHAIN (spec) = list;
2105 list = spec;
2109 /* Keep the noexcept-specifier at the beginning of the list. */
2110 if (noex != TREE_PURPOSE (list))
2111 list = tree_cons (noex, TREE_VALUE (list), TREE_CHAIN (list));
2113 return list;
2116 /* Subroutine of build_call. Ensure that each of the types in the
2117 exception specification is complete. Technically, 15.4/1 says that
2118 they need to be complete when we see a declaration of the function,
2119 but we should be able to get away with only requiring this when the
2120 function is defined or called. See also add_exception_specifier. */
2122 void
2123 require_complete_eh_spec_types (tree fntype, tree decl)
2125 tree raises;
2126 /* Don't complain about calls to op new. */
2127 if (decl && DECL_ARTIFICIAL (decl))
2128 return;
2129 for (raises = TYPE_RAISES_EXCEPTIONS (fntype); raises;
2130 raises = TREE_CHAIN (raises))
2132 tree type = TREE_VALUE (raises);
2133 if (type && !COMPLETE_TYPE_P (type))
2135 if (decl)
2136 error
2137 ("call to function %qD which throws incomplete type %q#T",
2138 decl, type);
2139 else
2140 error ("call to function which throws incomplete type %q#T",
2141 decl);
2147 #include "gt-cp-typeck2.h"