[PATCH 9/13] x86 musl support
[official-gcc.git] / gcc / cp / typeck2.c
blob076f9a03498dc0025b0f71d8fb71f57f78f64684
1 /* Report error messages, build initializers, and perform
2 some front-end optimizations for C++ compiler.
3 Copyright (C) 1987-2015 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 "hash-set.h"
33 #include "machmode.h"
34 #include "vec.h"
35 #include "double-int.h"
36 #include "input.h"
37 #include "alias.h"
38 #include "symtab.h"
39 #include "wide-int.h"
40 #include "inchash.h"
41 #include "tree.h"
42 #include "stor-layout.h"
43 #include "varasm.h"
44 #include "intl.h"
45 #include "cp-tree.h"
46 #include "flags.h"
47 #include "diagnostic-core.h"
48 #include "wide-int.h"
50 static tree
51 process_init_constructor (tree type, tree init, tsubst_flags_t complain);
54 /* Print an error message stemming from an attempt to use
55 BASETYPE as a base class for TYPE. */
57 tree
58 error_not_base_type (tree basetype, tree type)
60 if (TREE_CODE (basetype) == FUNCTION_DECL)
61 basetype = DECL_CONTEXT (basetype);
62 error ("type %qT is not a base type for type %qT", basetype, type);
63 return error_mark_node;
66 tree
67 binfo_or_else (tree base, tree type)
69 tree binfo = lookup_base (type, base, ba_unique,
70 NULL, tf_warning_or_error);
72 if (binfo == error_mark_node)
73 return NULL_TREE;
74 else if (!binfo)
75 error_not_base_type (base, type);
76 return binfo;
79 /* According to ARM $7.1.6, "A `const' object may be initialized, but its
80 value may not be changed thereafter. */
82 void
83 cxx_readonly_error (tree arg, enum lvalue_use errstring)
86 /* This macro is used to emit diagnostics to ensure that all format
87 strings are complete sentences, visible to gettext and checked at
88 compile time. */
90 #define ERROR_FOR_ASSIGNMENT(AS, ASM, IN, DE, ARG) \
91 do { \
92 switch (errstring) \
93 { \
94 case lv_assign: \
95 error(AS, ARG); \
96 break; \
97 case lv_asm: \
98 error(ASM, ARG); \
99 break; \
100 case lv_increment: \
101 error (IN, ARG); \
102 break; \
103 case lv_decrement: \
104 error (DE, ARG); \
105 break; \
106 default: \
107 gcc_unreachable (); \
109 } while (0)
111 /* Handle C++-specific things first. */
113 if (VAR_P (arg)
114 && DECL_LANG_SPECIFIC (arg)
115 && DECL_IN_AGGR_P (arg)
116 && !TREE_STATIC (arg))
117 ERROR_FOR_ASSIGNMENT (G_("assignment of "
118 "constant field %qD"),
119 G_("constant field %qD "
120 "used as %<asm%> output"),
121 G_("increment of "
122 "constant field %qD"),
123 G_("decrement of "
124 "constant field %qD"),
125 arg);
126 else if (INDIRECT_REF_P (arg)
127 && TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 0))) == REFERENCE_TYPE
128 && (VAR_P (TREE_OPERAND (arg, 0))
129 || TREE_CODE (TREE_OPERAND (arg, 0)) == PARM_DECL))
130 ERROR_FOR_ASSIGNMENT (G_("assignment of "
131 "read-only reference %qD"),
132 G_("read-only reference %qD "
133 "used as %<asm%> output"),
134 G_("increment of "
135 "read-only reference %qD"),
136 G_("decrement of "
137 "read-only reference %qD"),
138 TREE_OPERAND (arg, 0));
139 else
140 readonly_error (input_location, arg, errstring);
144 /* Structure that holds information about declarations whose type was
145 incomplete and we could not check whether it was abstract or not. */
147 struct GTY((chain_next ("%h.next"), for_user)) pending_abstract_type {
148 /* Declaration which we are checking for abstractness. It is either
149 a DECL node, or an IDENTIFIER_NODE if we do not have a full
150 declaration available. */
151 tree decl;
153 /* Type which will be checked for abstractness. */
154 tree type;
156 /* Kind of use in an unnamed declarator. */
157 abstract_class_use use;
159 /* Position of the declaration. This is only needed for IDENTIFIER_NODEs,
160 because DECLs already carry locus information. */
161 location_t locus;
163 /* Link to the next element in list. */
164 struct pending_abstract_type* next;
167 struct abstract_type_hasher : ggc_hasher<pending_abstract_type *>
169 typedef tree compare_type;
170 static hashval_t hash (pending_abstract_type *);
171 static bool equal (pending_abstract_type *, tree);
174 /* Compute the hash value of the node VAL. This function is used by the
175 hash table abstract_pending_vars. */
177 hashval_t
178 abstract_type_hasher::hash (pending_abstract_type *pat)
180 return (hashval_t) TYPE_UID (pat->type);
184 /* Compare node VAL1 with the type VAL2. This function is used by the
185 hash table abstract_pending_vars. */
187 bool
188 abstract_type_hasher::equal (pending_abstract_type *pat1, tree type2)
190 return (pat1->type == type2);
193 /* Hash table that maintains pending_abstract_type nodes, for which we still
194 need to check for type abstractness. The key of the table is the type
195 of the declaration. */
196 static GTY (()) hash_table<abstract_type_hasher> *abstract_pending_vars = NULL;
198 static int abstract_virtuals_error_sfinae (tree, tree, abstract_class_use, tsubst_flags_t);
200 /* This function is called after TYPE is completed, and will check if there
201 are pending declarations for which we still need to verify the abstractness
202 of TYPE, and emit a diagnostic (through abstract_virtuals_error) if TYPE
203 turned out to be incomplete. */
205 void
206 complete_type_check_abstract (tree type)
208 struct pending_abstract_type *pat;
209 location_t cur_loc = input_location;
211 gcc_assert (COMPLETE_TYPE_P (type));
213 if (!abstract_pending_vars)
214 return;
216 /* Retrieve the list of pending declarations for this type. */
217 pending_abstract_type **slot
218 = abstract_pending_vars->find_slot_with_hash (type, TYPE_UID (type),
219 NO_INSERT);
220 if (!slot)
221 return;
222 pat = *slot;
223 gcc_assert (pat);
225 /* If the type is not abstract, do not do anything. */
226 if (CLASSTYPE_PURE_VIRTUALS (type))
228 struct pending_abstract_type *prev = 0, *next;
230 /* Reverse the list to emit the errors in top-down order. */
231 for (; pat; pat = next)
233 next = pat->next;
234 pat->next = prev;
235 prev = pat;
237 pat = prev;
239 /* Go through the list, and call abstract_virtuals_error for each
240 element: it will issue a diagnostic if the type is abstract. */
241 while (pat)
243 gcc_assert (type == pat->type);
245 /* Tweak input_location so that the diagnostic appears at the correct
246 location. Notice that this is only needed if the decl is an
247 IDENTIFIER_NODE. */
248 input_location = pat->locus;
249 abstract_virtuals_error_sfinae (pat->decl, pat->type, pat->use,
250 tf_warning_or_error);
251 pat = pat->next;
255 abstract_pending_vars->clear_slot (slot);
257 input_location = cur_loc;
261 /* If TYPE has abstract virtual functions, issue an error about trying
262 to create an object of that type. DECL is the object declared, or
263 NULL_TREE if the declaration is unavailable, in which case USE specifies
264 the kind of invalid use. Returns 1 if an error occurred; zero if
265 all was well. */
267 static int
268 abstract_virtuals_error_sfinae (tree decl, tree type, abstract_class_use use,
269 tsubst_flags_t complain)
271 vec<tree, va_gc> *pure;
273 /* This function applies only to classes. Any other entity can never
274 be abstract. */
275 if (!CLASS_TYPE_P (type))
276 return 0;
277 type = TYPE_MAIN_VARIANT (type);
279 #if 0
280 /* Instantiation here seems to be required by the standard,
281 but breaks e.g. boost::bind. FIXME! */
282 /* In SFINAE, non-N3276 context, force instantiation. */
283 if (!(complain & (tf_error|tf_decltype)))
284 complete_type (type);
285 #endif
287 /* If the type is incomplete, we register it within a hash table,
288 so that we can check again once it is completed. This makes sense
289 only for objects for which we have a declaration or at least a
290 name. */
291 if (!COMPLETE_TYPE_P (type) && (complain & tf_error))
293 struct pending_abstract_type *pat;
295 gcc_assert (!decl || DECL_P (decl) || identifier_p (decl));
297 if (!abstract_pending_vars)
298 abstract_pending_vars
299 = hash_table<abstract_type_hasher>::create_ggc (31);
301 pending_abstract_type **slot
302 = abstract_pending_vars->find_slot_with_hash (type, TYPE_UID (type),
303 INSERT);
305 pat = ggc_alloc<pending_abstract_type> ();
306 pat->type = type;
307 pat->decl = decl;
308 pat->use = use;
309 pat->locus = ((decl && DECL_P (decl))
310 ? DECL_SOURCE_LOCATION (decl)
311 : input_location);
313 pat->next = *slot;
314 *slot = pat;
316 return 0;
319 if (!TYPE_SIZE (type))
320 /* TYPE is being defined, and during that time
321 CLASSTYPE_PURE_VIRTUALS holds the inline friends. */
322 return 0;
324 pure = CLASSTYPE_PURE_VIRTUALS (type);
325 if (!pure)
326 return 0;
328 if (!(complain & tf_error))
329 return 1;
331 if (decl)
333 if (VAR_P (decl))
334 error ("cannot declare variable %q+D to be of abstract "
335 "type %qT", decl, type);
336 else if (TREE_CODE (decl) == PARM_DECL)
338 if (DECL_NAME (decl))
339 error ("cannot declare parameter %q+D to be of abstract type %qT",
340 decl, type);
341 else
342 error ("cannot declare parameter to be of abstract type %qT",
343 type);
345 else if (TREE_CODE (decl) == FIELD_DECL)
346 error ("cannot declare field %q+D to be of abstract type %qT",
347 decl, type);
348 else if (TREE_CODE (decl) == FUNCTION_DECL
349 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
350 error ("invalid abstract return type for member function %q+#D", decl);
351 else if (TREE_CODE (decl) == FUNCTION_DECL)
352 error ("invalid abstract return type for function %q+#D", decl);
353 else if (identifier_p (decl))
354 /* Here we do not have location information. */
355 error ("invalid abstract type %qT for %qE", type, decl);
356 else
357 error ("invalid abstract type for %q+D", decl);
359 else switch (use)
361 case ACU_ARRAY:
362 error ("creating array of %qT, which is an abstract class type", type);
363 break;
364 case ACU_CAST:
365 error ("invalid cast to abstract class type %qT", type);
366 break;
367 case ACU_NEW:
368 error ("invalid new-expression of abstract class type %qT", type);
369 break;
370 case ACU_RETURN:
371 error ("invalid abstract return type %qT", type);
372 break;
373 case ACU_PARM:
374 error ("invalid abstract parameter type %qT", type);
375 break;
376 case ACU_THROW:
377 error ("expression of abstract class type %qT cannot "
378 "be used in throw-expression", type);
379 break;
380 case ACU_CATCH:
381 error ("cannot declare catch parameter to be of abstract "
382 "class type %qT", type);
383 break;
384 default:
385 error ("cannot allocate an object of abstract type %qT", type);
388 /* Only go through this once. */
389 if (pure->length ())
391 unsigned ix;
392 tree fn;
394 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
395 " because the following virtual functions are pure within %qT:",
396 type);
398 FOR_EACH_VEC_ELT (*pure, ix, fn)
399 if (! DECL_CLONED_FUNCTION_P (fn)
400 || DECL_COMPLETE_DESTRUCTOR_P (fn))
401 inform (input_location, "\t%+#D", fn);
403 /* Now truncate the vector. This leaves it non-null, so we know
404 there are pure virtuals, but empty so we don't list them out
405 again. */
406 pure->truncate (0);
409 return 1;
413 abstract_virtuals_error_sfinae (tree decl, tree type, tsubst_flags_t complain)
415 return abstract_virtuals_error_sfinae (decl, type, ACU_UNKNOWN, complain);
419 abstract_virtuals_error_sfinae (abstract_class_use use, tree type,
420 tsubst_flags_t complain)
422 return abstract_virtuals_error_sfinae (NULL_TREE, type, use, complain);
426 /* Wrapper for the above function in the common case of wanting errors. */
429 abstract_virtuals_error (tree decl, tree type)
431 return abstract_virtuals_error_sfinae (decl, type, tf_warning_or_error);
435 abstract_virtuals_error (abstract_class_use use, tree type)
437 return abstract_virtuals_error_sfinae (use, type, tf_warning_or_error);
440 /* Print an inform about the declaration of the incomplete type TYPE. */
442 void
443 cxx_incomplete_type_inform (const_tree type)
445 if (!TYPE_MAIN_DECL (type))
446 return;
448 location_t loc = DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type));
449 tree ptype = strip_top_quals (CONST_CAST_TREE (type));
451 if (current_class_type
452 && TYPE_BEING_DEFINED (current_class_type)
453 && same_type_p (ptype, current_class_type))
454 inform (loc, "definition of %q#T is not complete until "
455 "the closing brace", ptype);
456 else if (!TYPE_TEMPLATE_INFO (ptype))
457 inform (loc, "forward declaration of %q#T", ptype);
458 else
459 inform (loc, "declaration of %q#T", ptype);
462 /* Print an error message for invalid use of an incomplete type.
463 VALUE is the expression that was used (or 0 if that isn't known)
464 and TYPE is the type that was invalid. DIAG_KIND indicates the
465 type of diagnostic (see diagnostic.def). */
467 void
468 cxx_incomplete_type_diagnostic (const_tree value, const_tree type,
469 diagnostic_t diag_kind)
471 bool is_decl = false, complained = false;
473 gcc_assert (diag_kind == DK_WARNING
474 || diag_kind == DK_PEDWARN
475 || diag_kind == DK_ERROR);
477 /* Avoid duplicate error message. */
478 if (TREE_CODE (type) == ERROR_MARK)
479 return;
481 if (value != 0 && (VAR_P (value)
482 || TREE_CODE (value) == PARM_DECL
483 || TREE_CODE (value) == FIELD_DECL))
485 complained = emit_diagnostic (diag_kind, input_location, 0,
486 "%q+D has incomplete type", value);
487 is_decl = true;
489 retry:
490 /* We must print an error message. Be clever about what it says. */
492 switch (TREE_CODE (type))
494 case RECORD_TYPE:
495 case UNION_TYPE:
496 case ENUMERAL_TYPE:
497 if (!is_decl)
498 complained = emit_diagnostic (diag_kind, input_location, 0,
499 "invalid use of incomplete type %q#T",
500 type);
501 if (complained)
502 cxx_incomplete_type_inform (type);
503 break;
505 case VOID_TYPE:
506 emit_diagnostic (diag_kind, input_location, 0,
507 "invalid use of %qT", type);
508 break;
510 case ARRAY_TYPE:
511 if (TYPE_DOMAIN (type))
513 type = TREE_TYPE (type);
514 goto retry;
516 emit_diagnostic (diag_kind, input_location, 0,
517 "invalid use of array with unspecified bounds");
518 break;
520 case OFFSET_TYPE:
521 bad_member:
523 tree member = TREE_OPERAND (value, 1);
524 if (is_overloaded_fn (member))
525 member = get_first_fn (member);
526 if (DECL_FUNCTION_MEMBER_P (member)
527 && ! flag_ms_extensions)
528 emit_diagnostic (diag_kind, input_location, 0,
529 "invalid use of member function "
530 "(did you forget the %<()%> ?)");
531 else
532 emit_diagnostic (diag_kind, input_location, 0,
533 "invalid use of member "
534 "(did you forget the %<&%> ?)");
536 break;
538 case TEMPLATE_TYPE_PARM:
539 if (is_auto (type))
540 emit_diagnostic (diag_kind, input_location, 0,
541 "invalid use of %<auto%>");
542 else
543 emit_diagnostic (diag_kind, input_location, 0,
544 "invalid use of template type parameter %qT", type);
545 break;
547 case BOUND_TEMPLATE_TEMPLATE_PARM:
548 emit_diagnostic (diag_kind, input_location, 0,
549 "invalid use of template template parameter %qT",
550 TYPE_NAME (type));
551 break;
553 case TYPENAME_TYPE:
554 emit_diagnostic (diag_kind, input_location, 0,
555 "invalid use of dependent type %qT", type);
556 break;
558 case LANG_TYPE:
559 if (type == init_list_type_node)
561 emit_diagnostic (diag_kind, input_location, 0,
562 "invalid use of brace-enclosed initializer list");
563 break;
565 gcc_assert (type == unknown_type_node);
566 if (value && TREE_CODE (value) == COMPONENT_REF)
567 goto bad_member;
568 else if (value && TREE_CODE (value) == ADDR_EXPR)
569 emit_diagnostic (diag_kind, input_location, 0,
570 "address of overloaded function with no contextual "
571 "type information");
572 else if (value && TREE_CODE (value) == OVERLOAD)
573 emit_diagnostic (diag_kind, input_location, 0,
574 "overloaded function with no contextual type information");
575 else
576 emit_diagnostic (diag_kind, input_location, 0,
577 "insufficient contextual information to determine type");
578 break;
580 default:
581 gcc_unreachable ();
585 /* Backward-compatibility interface to incomplete_type_diagnostic;
586 required by ../tree.c. */
587 #undef cxx_incomplete_type_error
588 void
589 cxx_incomplete_type_error (const_tree value, const_tree type)
591 cxx_incomplete_type_diagnostic (value, type, DK_ERROR);
595 /* The recursive part of split_nonconstant_init. DEST is an lvalue
596 expression to which INIT should be assigned. INIT is a CONSTRUCTOR.
597 Return true if the whole of the value was initialized by the
598 generated statements. */
600 static bool
601 split_nonconstant_init_1 (tree dest, tree init)
603 unsigned HOST_WIDE_INT idx;
604 tree field_index, value;
605 tree type = TREE_TYPE (dest);
606 tree inner_type = NULL;
607 bool array_type_p = false;
608 bool complete_p = true;
609 HOST_WIDE_INT num_split_elts = 0;
611 switch (TREE_CODE (type))
613 case ARRAY_TYPE:
614 inner_type = TREE_TYPE (type);
615 array_type_p = true;
616 if ((TREE_SIDE_EFFECTS (init)
617 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
618 || array_of_runtime_bound_p (type))
620 /* For an array, we only need/want a single cleanup region rather
621 than one per element. */
622 tree code = build_vec_init (dest, NULL_TREE, init, false, 1,
623 tf_warning_or_error);
624 add_stmt (code);
625 return true;
627 /* FALLTHRU */
629 case RECORD_TYPE:
630 case UNION_TYPE:
631 case QUAL_UNION_TYPE:
632 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), idx,
633 field_index, value)
635 /* The current implementation of this algorithm assumes that
636 the field was set for all the elements. This is usually done
637 by process_init_constructor. */
638 gcc_assert (field_index);
640 if (!array_type_p)
641 inner_type = TREE_TYPE (field_index);
643 if (TREE_CODE (value) == CONSTRUCTOR)
645 tree sub;
647 if (array_type_p)
648 sub = build4 (ARRAY_REF, inner_type, dest, field_index,
649 NULL_TREE, NULL_TREE);
650 else
651 sub = build3 (COMPONENT_REF, inner_type, dest, field_index,
652 NULL_TREE);
654 if (!split_nonconstant_init_1 (sub, value))
655 complete_p = false;
656 else
657 CONSTRUCTOR_ELTS (init)->ordered_remove (idx--);
658 num_split_elts++;
660 else if (!initializer_constant_valid_p (value, inner_type))
662 tree code;
663 tree sub;
665 /* FIXME: Ordered removal is O(1) so the whole function is
666 worst-case quadratic. This could be fixed using an aside
667 bitmap to record which elements must be removed and remove
668 them all at the same time. Or by merging
669 split_non_constant_init into process_init_constructor_array,
670 that is separating constants from non-constants while building
671 the vector. */
672 CONSTRUCTOR_ELTS (init)->ordered_remove (idx);
673 --idx;
675 if (TREE_CODE (field_index) == RANGE_EXPR)
677 /* Use build_vec_init to initialize a range. */
678 tree low = TREE_OPERAND (field_index, 0);
679 tree hi = TREE_OPERAND (field_index, 1);
680 sub = build4 (ARRAY_REF, inner_type, dest, low,
681 NULL_TREE, NULL_TREE);
682 sub = cp_build_addr_expr (sub, tf_warning_or_error);
683 tree max = size_binop (MINUS_EXPR, hi, low);
684 code = build_vec_init (sub, max, value, false, 0,
685 tf_warning_or_error);
686 add_stmt (code);
687 if (tree_fits_shwi_p (max))
688 num_split_elts += tree_to_shwi (max);
690 else
692 if (array_type_p)
693 sub = build4 (ARRAY_REF, inner_type, dest, field_index,
694 NULL_TREE, NULL_TREE);
695 else
696 sub = build3 (COMPONENT_REF, inner_type, dest, field_index,
697 NULL_TREE);
699 code = build2 (INIT_EXPR, inner_type, sub, value);
700 code = build_stmt (input_location, EXPR_STMT, code);
701 code = maybe_cleanup_point_expr_void (code);
702 add_stmt (code);
703 if (type_build_dtor_call (inner_type))
705 code = (build_special_member_call
706 (sub, complete_dtor_identifier, NULL, inner_type,
707 LOOKUP_NORMAL, tf_warning_or_error));
708 if (!TYPE_HAS_TRIVIAL_DESTRUCTOR (inner_type))
709 finish_eh_cleanup (code);
713 num_split_elts++;
716 break;
718 case VECTOR_TYPE:
719 if (!initializer_constant_valid_p (init, type))
721 tree code;
722 tree cons = copy_node (init);
723 CONSTRUCTOR_ELTS (init) = NULL;
724 code = build2 (MODIFY_EXPR, type, dest, cons);
725 code = build_stmt (input_location, EXPR_STMT, code);
726 add_stmt (code);
727 num_split_elts += CONSTRUCTOR_NELTS (init);
729 break;
731 default:
732 gcc_unreachable ();
735 /* The rest of the initializer is now a constant. */
736 TREE_CONSTANT (init) = 1;
737 return complete_p && complete_ctor_at_level_p (TREE_TYPE (init),
738 num_split_elts, inner_type);
741 /* A subroutine of store_init_value. Splits non-constant static
742 initializer INIT into a constant part and generates code to
743 perform the non-constant part of the initialization to DEST.
744 Returns the code for the runtime init. */
746 tree
747 split_nonconstant_init (tree dest, tree init)
749 tree code;
751 if (TREE_CODE (init) == TARGET_EXPR)
752 init = TARGET_EXPR_INITIAL (init);
753 if (TREE_CODE (init) == CONSTRUCTOR)
755 code = push_stmt_list ();
756 if (split_nonconstant_init_1 (dest, init))
757 init = NULL_TREE;
758 code = pop_stmt_list (code);
759 DECL_INITIAL (dest) = init;
760 TREE_READONLY (dest) = 0;
762 else
763 code = build2 (INIT_EXPR, TREE_TYPE (dest), dest, init);
765 return code;
768 /* Perform appropriate conversions on the initial value of a variable,
769 store it in the declaration DECL,
770 and print any error messages that are appropriate.
771 If the init is invalid, store an ERROR_MARK.
773 C++: Note that INIT might be a TREE_LIST, which would mean that it is
774 a base class initializer for some aggregate type, hopefully compatible
775 with DECL. If INIT is a single element, and DECL is an aggregate
776 type, we silently convert INIT into a TREE_LIST, allowing a constructor
777 to be called.
779 If INIT is a TREE_LIST and there is no constructor, turn INIT
780 into a CONSTRUCTOR and use standard initialization techniques.
781 Perhaps a warning should be generated?
783 Returns code to be executed if initialization could not be performed
784 for static variable. In that case, caller must emit the code. */
786 tree
787 store_init_value (tree decl, tree init, vec<tree, va_gc>** cleanups, int flags)
789 tree value, type;
791 /* If variable's type was invalidly declared, just ignore it. */
793 type = TREE_TYPE (decl);
794 if (TREE_CODE (type) == ERROR_MARK)
795 return NULL_TREE;
797 if (MAYBE_CLASS_TYPE_P (type))
799 if (TREE_CODE (init) == TREE_LIST)
801 error ("constructor syntax used, but no constructor declared "
802 "for type %qT", type);
803 init = build_constructor_from_list (init_list_type_node, nreverse (init));
807 /* End of special C++ code. */
809 if (flags & LOOKUP_ALREADY_DIGESTED)
810 value = init;
811 else
812 /* Digest the specified initializer into an expression. */
813 value = digest_init_flags (type, init, flags);
815 value = extend_ref_init_temps (decl, value, cleanups);
817 /* In C++11 constant expression is a semantic, not syntactic, property.
818 In C++98, make sure that what we thought was a constant expression at
819 template definition time is still constant and otherwise perform this
820 as optimization, e.g. to fold SIZEOF_EXPRs in the initializer. */
821 if (decl_maybe_constant_var_p (decl) || TREE_STATIC (decl))
823 bool const_init;
824 value = instantiate_non_dependent_expr (value);
825 if (DECL_DECLARED_CONSTEXPR_P (decl)
826 || DECL_IN_AGGR_P (decl))
828 /* Diagnose a non-constant initializer for constexpr. */
829 if (processing_template_decl
830 && !require_potential_constant_expression (value))
831 value = error_mark_node;
832 else
833 value = cxx_constant_value (value, decl);
835 value = maybe_constant_init (value, decl);
836 if (TREE_CODE (value) == CONSTRUCTOR && cp_has_mutable_p (type))
837 /* Poison this CONSTRUCTOR so it can't be copied to another
838 constexpr variable. */
839 CONSTRUCTOR_MUTABLE_POISON (value) = true;
840 const_init = (reduced_constant_expression_p (value)
841 || error_operand_p (value));
842 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = const_init;
843 TREE_CONSTANT (decl) = const_init && decl_maybe_constant_var_p (decl);
846 if (cxx_dialect >= cxx14)
847 /* Handle aggregate NSDMI in non-constant initializers, too. */
848 value = replace_placeholders (value, decl);
850 /* If the initializer is not a constant, fill in DECL_INITIAL with
851 the bits that are constant, and then return an expression that
852 will perform the dynamic initialization. */
853 if (value != error_mark_node
854 && (TREE_SIDE_EFFECTS (value)
855 || array_of_runtime_bound_p (type)
856 || ! reduced_constant_expression_p (value)))
857 return split_nonconstant_init (decl, value);
858 /* If the value is a constant, just put it in DECL_INITIAL. If DECL
859 is an automatic variable, the middle end will turn this into a
860 dynamic initialization later. */
861 DECL_INITIAL (decl) = value;
862 return NULL_TREE;
866 /* Give diagnostic about narrowing conversions within { }. */
868 bool
869 check_narrowing (tree type, tree init, tsubst_flags_t complain)
871 tree ftype = unlowered_expr_type (init);
872 bool ok = true;
873 REAL_VALUE_TYPE d;
875 if (((!warn_narrowing || !(complain & tf_warning))
876 && cxx_dialect == cxx98)
877 || !ARITHMETIC_TYPE_P (type))
878 return ok;
880 if (BRACE_ENCLOSED_INITIALIZER_P (init)
881 && TREE_CODE (type) == COMPLEX_TYPE)
883 tree elttype = TREE_TYPE (type);
884 if (CONSTRUCTOR_NELTS (init) > 0)
885 ok &= check_narrowing (elttype, CONSTRUCTOR_ELT (init, 0)->value,
886 complain);
887 if (CONSTRUCTOR_NELTS (init) > 1)
888 ok &= check_narrowing (elttype, CONSTRUCTOR_ELT (init, 1)->value,
889 complain);
890 return ok;
893 init = fold_non_dependent_expr (init);
895 if (TREE_CODE (type) == INTEGER_TYPE
896 && TREE_CODE (ftype) == REAL_TYPE)
897 ok = false;
898 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype)
899 && CP_INTEGRAL_TYPE_P (type))
901 if (TREE_CODE (ftype) == ENUMERAL_TYPE)
902 /* Check for narrowing based on the values of the enumeration. */
903 ftype = ENUM_UNDERLYING_TYPE (ftype);
904 if ((tree_int_cst_lt (TYPE_MAX_VALUE (type),
905 TYPE_MAX_VALUE (ftype))
906 || tree_int_cst_lt (TYPE_MIN_VALUE (ftype),
907 TYPE_MIN_VALUE (type)))
908 && (TREE_CODE (init) != INTEGER_CST
909 || !int_fits_type_p (init, type)))
910 ok = false;
912 else if (TREE_CODE (ftype) == REAL_TYPE
913 && TREE_CODE (type) == REAL_TYPE)
915 if (TYPE_PRECISION (type) < TYPE_PRECISION (ftype))
917 if (TREE_CODE (init) == REAL_CST)
919 /* Issue 703: Loss of precision is OK as long as the value is
920 within the representable range of the new type. */
921 REAL_VALUE_TYPE r;
922 d = TREE_REAL_CST (init);
923 real_convert (&r, TYPE_MODE (type), &d);
924 if (real_isinf (&r))
925 ok = false;
927 else
928 ok = false;
931 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype)
932 && TREE_CODE (type) == REAL_TYPE)
934 ok = false;
935 if (TREE_CODE (init) == INTEGER_CST)
937 d = real_value_from_int_cst (0, init);
938 if (exact_real_truncate (TYPE_MODE (type), &d))
939 ok = true;
943 if (!ok)
945 if (cxx_dialect == cxx98)
946 warning_at (EXPR_LOC_OR_LOC (init, input_location), OPT_Wnarrowing,
947 "narrowing conversion of %qE from %qT to %qT inside { } "
948 "is ill-formed in C++11", init, ftype, type);
949 else if (!TREE_CONSTANT (init))
951 if (complain & tf_warning_or_error)
953 pedwarn (EXPR_LOC_OR_LOC (init, input_location), OPT_Wnarrowing,
954 "narrowing conversion of %qE from %qT to %qT inside { }",
955 init, ftype, type);
956 ok = true;
959 else if (complain & tf_error)
961 int savederrorcount = errorcount;
962 global_dc->pedantic_errors = 1;
963 pedwarn (EXPR_LOC_OR_LOC (init, input_location), OPT_Wnarrowing,
964 "narrowing conversion of %qE from %qT to %qT "
965 "inside { }", init, ftype, type);
966 if (errorcount == savederrorcount)
967 ok = true;
968 global_dc->pedantic_errors = flag_pedantic_errors;
972 return cxx_dialect == cxx98 || ok;
975 /* Process the initializer INIT for a variable of type TYPE, emitting
976 diagnostics for invalid initializers and converting the initializer as
977 appropriate.
979 For aggregate types, it assumes that reshape_init has already run, thus the
980 initializer will have the right shape (brace elision has been undone).
982 NESTED is true iff we are being called for an element of a CONSTRUCTOR. */
984 static tree
985 digest_init_r (tree type, tree init, bool nested, int flags,
986 tsubst_flags_t complain)
988 enum tree_code code = TREE_CODE (type);
990 if (error_operand_p (init))
991 return error_mark_node;
993 gcc_assert (init);
995 /* We must strip the outermost array type when completing the type,
996 because the its bounds might be incomplete at the moment. */
997 if (!complete_type_or_maybe_complain (TREE_CODE (type) == ARRAY_TYPE
998 ? TREE_TYPE (type) : type, NULL_TREE,
999 complain))
1000 return error_mark_node;
1002 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue
1003 (g++.old-deja/g++.law/casts2.C). */
1004 if (TREE_CODE (init) == NON_LVALUE_EXPR)
1005 init = TREE_OPERAND (init, 0);
1007 /* Initialization of an array of chars from a string constant. The initializer
1008 can be optionally enclosed in braces, but reshape_init has already removed
1009 them if they were present. */
1010 if (code == ARRAY_TYPE)
1012 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
1013 if (char_type_p (typ1)
1014 /*&& init */
1015 && TREE_CODE (init) == STRING_CST)
1017 tree char_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (init)));
1019 if (TYPE_PRECISION (typ1) == BITS_PER_UNIT)
1021 if (char_type != char_type_node)
1023 if (complain & tf_error)
1024 error ("char-array initialized from wide string");
1025 return error_mark_node;
1028 else
1030 if (char_type == char_type_node)
1032 if (complain & tf_error)
1033 error ("int-array initialized from non-wide string");
1034 return error_mark_node;
1036 else if (char_type != typ1)
1038 if (complain & tf_error)
1039 error ("int-array initialized from incompatible "
1040 "wide string");
1041 return error_mark_node;
1045 if (type != TREE_TYPE (init))
1047 init = copy_node (init);
1048 TREE_TYPE (init) = type;
1050 if (TYPE_DOMAIN (type) != 0 && TREE_CONSTANT (TYPE_SIZE (type)))
1052 int size = TREE_INT_CST_LOW (TYPE_SIZE (type));
1053 size = (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
1054 /* In C it is ok to subtract 1 from the length of the string
1055 because it's ok to ignore the terminating null char that is
1056 counted in the length of the constant, but in C++ this would
1057 be invalid. */
1058 if (size < TREE_STRING_LENGTH (init))
1059 permerror (input_location, "initializer-string for array "
1060 "of chars is too long");
1062 return init;
1066 /* Handle scalar types (including conversions) and references. */
1067 if ((TREE_CODE (type) != COMPLEX_TYPE
1068 || BRACE_ENCLOSED_INITIALIZER_P (init))
1069 && (SCALAR_TYPE_P (type) || code == REFERENCE_TYPE))
1071 tree *exp;
1073 if (nested)
1074 flags |= LOOKUP_NO_NARROWING;
1075 init = convert_for_initialization (0, type, init, flags,
1076 ICR_INIT, NULL_TREE, 0,
1077 complain);
1078 exp = &init;
1080 /* Skip any conversions since we'll be outputting the underlying
1081 constant. */
1082 while (CONVERT_EXPR_P (*exp)
1083 || TREE_CODE (*exp) == NON_LVALUE_EXPR)
1084 exp = &TREE_OPERAND (*exp, 0);
1086 *exp = cplus_expand_constant (*exp);
1088 return init;
1091 /* Come here only for aggregates: records, arrays, unions, complex numbers
1092 and vectors. */
1093 gcc_assert (TREE_CODE (type) == ARRAY_TYPE
1094 || TREE_CODE (type) == VECTOR_TYPE
1095 || TREE_CODE (type) == RECORD_TYPE
1096 || TREE_CODE (type) == UNION_TYPE
1097 || TREE_CODE (type) == COMPLEX_TYPE);
1099 /* "If T is a class type and the initializer list has a single
1100 element of type cv U, where U is T or a class derived from T,
1101 the object is initialized from that element." */
1102 if (cxx_dialect >= cxx11
1103 && BRACE_ENCLOSED_INITIALIZER_P (init)
1104 && CONSTRUCTOR_NELTS (init) == 1
1105 && (CLASS_TYPE_P (type) || VECTOR_TYPE_P (type)))
1107 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
1108 if (reference_related_p (type, TREE_TYPE (elt)))
1109 init = elt;
1112 if (BRACE_ENCLOSED_INITIALIZER_P (init)
1113 && !TYPE_NON_AGGREGATE_CLASS (type))
1114 return process_init_constructor (type, init, complain);
1115 else
1117 if (COMPOUND_LITERAL_P (init) && TREE_CODE (type) == ARRAY_TYPE)
1119 if (complain & tf_error)
1120 error ("cannot initialize aggregate of type %qT with "
1121 "a compound literal", type);
1123 return error_mark_node;
1126 if (TREE_CODE (type) == ARRAY_TYPE
1127 && !BRACE_ENCLOSED_INITIALIZER_P (init))
1129 /* Allow the result of build_array_copy and of
1130 build_value_init_noctor. */
1131 if ((TREE_CODE (init) == VEC_INIT_EXPR
1132 || TREE_CODE (init) == CONSTRUCTOR)
1133 && (same_type_ignoring_top_level_qualifiers_p
1134 (type, TREE_TYPE (init))))
1135 return init;
1137 if (complain & tf_error)
1138 error ("array must be initialized with a brace-enclosed"
1139 " initializer");
1140 return error_mark_node;
1143 return convert_for_initialization (NULL_TREE, type, init,
1144 flags,
1145 ICR_INIT, NULL_TREE, 0,
1146 complain);
1150 tree
1151 digest_init (tree type, tree init, tsubst_flags_t complain)
1153 return digest_init_r (type, init, false, LOOKUP_IMPLICIT, complain);
1156 tree
1157 digest_init_flags (tree type, tree init, int flags)
1159 return digest_init_r (type, init, false, flags, tf_warning_or_error);
1162 /* Process the initializer INIT for an NSDMI DECL (a FIELD_DECL). */
1163 tree
1164 digest_nsdmi_init (tree decl, tree init)
1166 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
1168 int flags = LOOKUP_IMPLICIT;
1169 if (DIRECT_LIST_INIT_P (init))
1170 flags = LOOKUP_NORMAL;
1171 init = digest_init_flags (TREE_TYPE (decl), init, flags);
1172 if (TREE_CODE (init) == TARGET_EXPR)
1173 /* This represents the whole initialization. */
1174 TARGET_EXPR_DIRECT_INIT_P (init) = true;
1175 return init;
1178 /* Set of flags used within process_init_constructor to describe the
1179 initializers. */
1180 #define PICFLAG_ERRONEOUS 1
1181 #define PICFLAG_NOT_ALL_CONSTANT 2
1182 #define PICFLAG_NOT_ALL_SIMPLE 4
1183 #define PICFLAG_SIDE_EFFECTS 8
1185 /* Given an initializer INIT, return the flag (PICFLAG_*) which better
1186 describe it. */
1188 static int
1189 picflag_from_initializer (tree init)
1191 if (init == error_mark_node)
1192 return PICFLAG_ERRONEOUS;
1193 else if (!TREE_CONSTANT (init))
1195 if (TREE_SIDE_EFFECTS (init))
1196 return PICFLAG_SIDE_EFFECTS;
1197 else
1198 return PICFLAG_NOT_ALL_CONSTANT;
1200 else if (!initializer_constant_valid_p (init, TREE_TYPE (init)))
1201 return PICFLAG_NOT_ALL_SIMPLE;
1202 return 0;
1205 /* Adjust INIT for going into a CONSTRUCTOR. */
1207 static tree
1208 massage_init_elt (tree type, tree init, tsubst_flags_t complain)
1210 init = digest_init_r (type, init, true, LOOKUP_IMPLICIT, complain);
1211 /* Strip a simple TARGET_EXPR when we know this is an initializer. */
1212 if (TREE_CODE (init) == TARGET_EXPR
1213 && !VOID_TYPE_P (TREE_TYPE (TARGET_EXPR_INITIAL (init))))
1214 init = TARGET_EXPR_INITIAL (init);
1215 /* When we defer constant folding within a statement, we may want to
1216 defer this folding as well. */
1217 tree t = fold_non_dependent_expr (init);
1218 t = maybe_constant_init (t);
1219 if (TREE_CONSTANT (t))
1220 init = t;
1221 return init;
1224 /* Subroutine of process_init_constructor, which will process an initializer
1225 INIT for an array or vector of type TYPE. Returns the flags (PICFLAG_*)
1226 which describe the initializers. */
1228 static int
1229 process_init_constructor_array (tree type, tree init,
1230 tsubst_flags_t complain)
1232 unsigned HOST_WIDE_INT i, len = 0;
1233 int flags = 0;
1234 bool unbounded = false;
1235 constructor_elt *ce;
1236 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (init);
1238 gcc_assert (TREE_CODE (type) == ARRAY_TYPE
1239 || TREE_CODE (type) == VECTOR_TYPE);
1241 if (TREE_CODE (type) == ARRAY_TYPE)
1243 tree domain = TYPE_DOMAIN (type);
1244 if (domain && TREE_CONSTANT (TYPE_MAX_VALUE (domain)))
1245 len = wi::ext (wi::to_offset (TYPE_MAX_VALUE (domain))
1246 - wi::to_offset (TYPE_MIN_VALUE (domain)) + 1,
1247 TYPE_PRECISION (TREE_TYPE (domain)),
1248 TYPE_SIGN (TREE_TYPE (domain))).to_uhwi ();
1249 else
1250 unbounded = true; /* Take as many as there are. */
1252 else
1253 /* Vectors are like simple fixed-size arrays. */
1254 len = TYPE_VECTOR_SUBPARTS (type);
1256 /* There must not be more initializers than needed. */
1257 if (!unbounded && vec_safe_length (v) > len)
1259 if (complain & tf_error)
1260 error ("too many initializers for %qT", type);
1261 else
1262 return PICFLAG_ERRONEOUS;
1265 FOR_EACH_VEC_SAFE_ELT (v, i, ce)
1267 if (ce->index)
1269 gcc_assert (TREE_CODE (ce->index) == INTEGER_CST);
1270 if (compare_tree_int (ce->index, i) != 0)
1272 ce->value = error_mark_node;
1273 sorry ("non-trivial designated initializers not supported");
1276 else
1277 ce->index = size_int (i);
1278 gcc_assert (ce->value);
1279 ce->value = massage_init_elt (TREE_TYPE (type), ce->value, complain);
1281 if (ce->value != error_mark_node)
1282 gcc_assert (same_type_ignoring_top_level_qualifiers_p
1283 (TREE_TYPE (type), TREE_TYPE (ce->value)));
1285 flags |= picflag_from_initializer (ce->value);
1288 /* No more initializers. If the array is unbounded, we are done. Otherwise,
1289 we must add initializers ourselves. */
1290 if (!unbounded)
1291 for (; i < len; ++i)
1293 tree next;
1295 if (type_build_ctor_call (TREE_TYPE (type)))
1297 /* If this type needs constructors run for default-initialization,
1298 we can't rely on the back end to do it for us, so make the
1299 initialization explicit by list-initializing from T{}. */
1300 next = build_constructor (init_list_type_node, NULL);
1301 next = massage_init_elt (TREE_TYPE (type), next, complain);
1302 if (initializer_zerop (next))
1303 /* The default zero-initialization is fine for us; don't
1304 add anything to the CONSTRUCTOR. */
1305 next = NULL_TREE;
1307 else if (!zero_init_p (TREE_TYPE (type)))
1308 next = build_zero_init (TREE_TYPE (type),
1309 /*nelts=*/NULL_TREE,
1310 /*static_storage_p=*/false);
1311 else
1312 /* The default zero-initialization is fine for us; don't
1313 add anything to the CONSTRUCTOR. */
1314 next = NULL_TREE;
1316 if (next)
1318 flags |= picflag_from_initializer (next);
1319 CONSTRUCTOR_APPEND_ELT (v, size_int (i), next);
1323 CONSTRUCTOR_ELTS (init) = v;
1324 return flags;
1327 /* Subroutine of process_init_constructor, which will process an initializer
1328 INIT for a class of type TYPE. Returns the flags (PICFLAG_*) which describe
1329 the initializers. */
1331 static int
1332 process_init_constructor_record (tree type, tree init,
1333 tsubst_flags_t complain)
1335 vec<constructor_elt, va_gc> *v = NULL;
1336 tree field;
1337 int skipped = 0;
1339 gcc_assert (TREE_CODE (type) == RECORD_TYPE);
1340 gcc_assert (!CLASSTYPE_VBASECLASSES (type));
1341 gcc_assert (!TYPE_BINFO (type)
1342 || !BINFO_N_BASE_BINFOS (TYPE_BINFO (type)));
1343 gcc_assert (!TYPE_POLYMORPHIC_P (type));
1345 restart:
1346 int flags = 0;
1347 unsigned HOST_WIDE_INT idx = 0;
1348 /* Generally, we will always have an index for each initializer (which is
1349 a FIELD_DECL, put by reshape_init), but compound literals don't go trough
1350 reshape_init. So we need to handle both cases. */
1351 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
1353 tree next;
1354 tree type;
1356 if (!DECL_NAME (field) && DECL_C_BIT_FIELD (field))
1357 continue;
1359 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
1360 continue;
1362 /* If this is a bitfield, first convert to the declared type. */
1363 type = TREE_TYPE (field);
1364 if (DECL_BIT_FIELD_TYPE (field))
1365 type = DECL_BIT_FIELD_TYPE (field);
1366 if (type == error_mark_node)
1367 return PICFLAG_ERRONEOUS;
1369 if (idx < vec_safe_length (CONSTRUCTOR_ELTS (init)))
1371 constructor_elt *ce = &(*CONSTRUCTOR_ELTS (init))[idx];
1372 if (ce->index)
1374 /* We can have either a FIELD_DECL or an IDENTIFIER_NODE. The
1375 latter case can happen in templates where lookup has to be
1376 deferred. */
1377 gcc_assert (TREE_CODE (ce->index) == FIELD_DECL
1378 || identifier_p (ce->index));
1379 if (ce->index != field
1380 && ce->index != DECL_NAME (field))
1382 ce->value = error_mark_node;
1383 sorry ("non-trivial designated initializers not supported");
1387 gcc_assert (ce->value);
1388 next = massage_init_elt (type, ce->value, complain);
1389 ++idx;
1391 else if (DECL_INITIAL (field))
1393 if (skipped > 0)
1395 /* We're using an NSDMI past a field with implicit
1396 zero-init. Go back and make it explicit. */
1397 skipped = -1;
1398 vec_safe_truncate (v, 0);
1399 goto restart;
1401 /* C++14 aggregate NSDMI. */
1402 next = get_nsdmi (field, /*ctor*/false);
1404 else if (type_build_ctor_call (TREE_TYPE (field)))
1406 /* If this type needs constructors run for
1407 default-initialization, we can't rely on the back end to do it
1408 for us, so build up TARGET_EXPRs. If the type in question is
1409 a class, just build one up; if it's an array, recurse. */
1410 next = build_constructor (init_list_type_node, NULL);
1411 next = massage_init_elt (TREE_TYPE (field), next, complain);
1413 /* Warn when some struct elements are implicitly initialized. */
1414 if ((complain & tf_warning)
1415 && !EMPTY_CONSTRUCTOR_P (init))
1416 warning (OPT_Wmissing_field_initializers,
1417 "missing initializer for member %qD", field);
1419 else
1421 if (TREE_CODE (TREE_TYPE (field)) == REFERENCE_TYPE)
1423 if (complain & tf_error)
1424 error ("member %qD is uninitialized reference", field);
1425 else
1426 return PICFLAG_ERRONEOUS;
1428 else if (CLASSTYPE_REF_FIELDS_NEED_INIT (TREE_TYPE (field)))
1430 if (complain & tf_error)
1431 error ("member %qD with uninitialized reference fields", field);
1432 else
1433 return PICFLAG_ERRONEOUS;
1436 /* Warn when some struct elements are implicitly initialized
1437 to zero. */
1438 if ((complain & tf_warning)
1439 && !EMPTY_CONSTRUCTOR_P (init))
1440 warning (OPT_Wmissing_field_initializers,
1441 "missing initializer for member %qD", field);
1443 if (!zero_init_p (TREE_TYPE (field))
1444 || skipped < 0)
1445 next = build_zero_init (TREE_TYPE (field), /*nelts=*/NULL_TREE,
1446 /*static_storage_p=*/false);
1447 else
1449 /* The default zero-initialization is fine for us; don't
1450 add anything to the CONSTRUCTOR. */
1451 skipped = 1;
1452 continue;
1456 /* If this is a bitfield, now convert to the lowered type. */
1457 if (type != TREE_TYPE (field))
1458 next = cp_convert_and_check (TREE_TYPE (field), next, complain);
1459 flags |= picflag_from_initializer (next);
1460 CONSTRUCTOR_APPEND_ELT (v, field, next);
1463 if (idx < vec_safe_length (CONSTRUCTOR_ELTS (init)))
1465 if (complain & tf_error)
1466 error ("too many initializers for %qT", type);
1467 else
1468 return PICFLAG_ERRONEOUS;
1471 CONSTRUCTOR_ELTS (init) = v;
1472 return flags;
1475 /* Subroutine of process_init_constructor, which will process a single
1476 initializer INIT for a union of type TYPE. Returns the flags (PICFLAG_*)
1477 which describe the initializer. */
1479 static int
1480 process_init_constructor_union (tree type, tree init,
1481 tsubst_flags_t complain)
1483 constructor_elt *ce;
1484 int len;
1486 /* If the initializer was empty, use default zero initialization. */
1487 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
1488 return 0;
1490 len = CONSTRUCTOR_ELTS (init)->length ();
1491 if (len > 1)
1493 if (!(complain & tf_error))
1494 return PICFLAG_ERRONEOUS;
1495 error ("too many initializers for %qT", type);
1496 CONSTRUCTOR_ELTS (init)->block_remove (1, len-1);
1499 ce = &(*CONSTRUCTOR_ELTS (init))[0];
1501 /* If this element specifies a field, initialize via that field. */
1502 if (ce->index)
1504 if (TREE_CODE (ce->index) == FIELD_DECL)
1506 else if (identifier_p (ce->index))
1508 /* This can happen within a cast, see g++.dg/opt/cse2.C. */
1509 tree name = ce->index;
1510 tree field;
1511 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1512 if (DECL_NAME (field) == name)
1513 break;
1514 if (!field)
1516 if (complain & tf_error)
1517 error ("no field %qD found in union being initialized",
1518 field);
1519 ce->value = error_mark_node;
1521 ce->index = field;
1523 else
1525 gcc_assert (TREE_CODE (ce->index) == INTEGER_CST
1526 || TREE_CODE (ce->index) == RANGE_EXPR);
1527 if (complain & tf_error)
1528 error ("index value instead of field name in union initializer");
1529 ce->value = error_mark_node;
1532 else
1534 /* Find the first named field. ANSI decided in September 1990
1535 that only named fields count here. */
1536 tree field = TYPE_FIELDS (type);
1537 while (field && (!DECL_NAME (field) || TREE_CODE (field) != FIELD_DECL))
1538 field = TREE_CHAIN (field);
1539 if (field == NULL_TREE)
1541 if (complain & tf_error)
1542 error ("too many initializers for %qT", type);
1543 ce->value = error_mark_node;
1545 ce->index = field;
1548 if (ce->value && ce->value != error_mark_node)
1549 ce->value = massage_init_elt (TREE_TYPE (ce->index), ce->value, complain);
1551 return picflag_from_initializer (ce->value);
1554 /* Process INIT, a constructor for a variable of aggregate type TYPE. The
1555 constructor is a brace-enclosed initializer, and will be modified in-place.
1557 Each element is converted to the right type through digest_init, and
1558 missing initializers are added following the language rules (zero-padding,
1559 etc.).
1561 After the execution, the initializer will have TREE_CONSTANT if all elts are
1562 constant, and TREE_STATIC set if, in addition, all elts are simple enough
1563 constants that the assembler and linker can compute them.
1565 The function returns the initializer itself, or error_mark_node in case
1566 of error. */
1568 static tree
1569 process_init_constructor (tree type, tree init, tsubst_flags_t complain)
1571 int flags;
1573 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
1575 if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == VECTOR_TYPE)
1576 flags = process_init_constructor_array (type, init, complain);
1577 else if (TREE_CODE (type) == RECORD_TYPE)
1578 flags = process_init_constructor_record (type, init, complain);
1579 else if (TREE_CODE (type) == UNION_TYPE)
1580 flags = process_init_constructor_union (type, init, complain);
1581 else
1582 gcc_unreachable ();
1584 if (flags & PICFLAG_ERRONEOUS)
1585 return error_mark_node;
1587 TREE_TYPE (init) = type;
1588 if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == NULL_TREE)
1589 cp_complete_array_type (&TREE_TYPE (init), init, /*do_default=*/0);
1590 if (flags & PICFLAG_SIDE_EFFECTS)
1592 TREE_CONSTANT (init) = false;
1593 TREE_SIDE_EFFECTS (init) = true;
1595 else if (flags & PICFLAG_NOT_ALL_CONSTANT)
1596 /* Make sure TREE_CONSTANT isn't set from build_constructor. */
1597 TREE_CONSTANT (init) = false;
1598 else
1600 TREE_CONSTANT (init) = 1;
1601 if (!(flags & PICFLAG_NOT_ALL_SIMPLE))
1602 TREE_STATIC (init) = 1;
1604 return init;
1607 /* Given a structure or union value DATUM, construct and return
1608 the structure or union component which results from narrowing
1609 that value to the base specified in BASETYPE. For example, given the
1610 hierarchy
1612 class L { int ii; };
1613 class A : L { ... };
1614 class B : L { ... };
1615 class C : A, B { ... };
1617 and the declaration
1619 C x;
1621 then the expression
1623 x.A::ii refers to the ii member of the L part of
1624 the A part of the C object named by X. In this case,
1625 DATUM would be x, and BASETYPE would be A.
1627 I used to think that this was nonconformant, that the standard specified
1628 that first we look up ii in A, then convert x to an L& and pull out the
1629 ii part. But in fact, it does say that we convert x to an A&; A here
1630 is known as the "naming class". (jason 2000-12-19)
1632 BINFO_P points to a variable initialized either to NULL_TREE or to the
1633 binfo for the specific base subobject we want to convert to. */
1635 tree
1636 build_scoped_ref (tree datum, tree basetype, tree* binfo_p)
1638 tree binfo;
1640 if (datum == error_mark_node)
1641 return error_mark_node;
1642 if (*binfo_p)
1643 binfo = *binfo_p;
1644 else
1645 binfo = lookup_base (TREE_TYPE (datum), basetype, ba_check,
1646 NULL, tf_warning_or_error);
1648 if (!binfo || binfo == error_mark_node)
1650 *binfo_p = NULL_TREE;
1651 if (!binfo)
1652 error_not_base_type (basetype, TREE_TYPE (datum));
1653 return error_mark_node;
1656 *binfo_p = binfo;
1657 return build_base_path (PLUS_EXPR, datum, binfo, 1,
1658 tf_warning_or_error);
1661 /* Build a reference to an object specified by the C++ `->' operator.
1662 Usually this just involves dereferencing the object, but if the
1663 `->' operator is overloaded, then such overloads must be
1664 performed until an object which does not have the `->' operator
1665 overloaded is found. An error is reported when circular pointer
1666 delegation is detected. */
1668 tree
1669 build_x_arrow (location_t loc, tree expr, tsubst_flags_t complain)
1671 tree orig_expr = expr;
1672 tree type = TREE_TYPE (expr);
1673 tree last_rval = NULL_TREE;
1674 vec<tree, va_gc> *types_memoized = NULL;
1676 if (type == error_mark_node)
1677 return error_mark_node;
1679 if (processing_template_decl)
1681 if (type_dependent_expression_p (expr))
1682 return build_min_nt_loc (loc, ARROW_EXPR, expr);
1683 expr = build_non_dependent_expr (expr);
1686 if (MAYBE_CLASS_TYPE_P (type))
1688 struct tinst_level *actual_inst = current_instantiation ();
1689 tree fn = NULL;
1691 while ((expr = build_new_op (loc, COMPONENT_REF,
1692 LOOKUP_NORMAL, expr, NULL_TREE, NULL_TREE,
1693 &fn, complain)))
1695 if (expr == error_mark_node)
1696 return error_mark_node;
1698 /* This provides a better instantiation backtrace in case of
1699 error. */
1700 if (fn && DECL_USE_TEMPLATE (fn))
1701 push_tinst_level_loc (fn,
1702 (current_instantiation () != actual_inst)
1703 ? DECL_SOURCE_LOCATION (fn)
1704 : input_location);
1705 fn = NULL;
1707 if (vec_member (TREE_TYPE (expr), types_memoized))
1709 if (complain & tf_error)
1710 error ("circular pointer delegation detected");
1711 return error_mark_node;
1714 vec_safe_push (types_memoized, TREE_TYPE (expr));
1715 last_rval = expr;
1718 while (current_instantiation () != actual_inst)
1719 pop_tinst_level ();
1721 if (last_rval == NULL_TREE)
1723 if (complain & tf_error)
1724 error ("base operand of %<->%> has non-pointer type %qT", type);
1725 return error_mark_node;
1728 if (TREE_CODE (TREE_TYPE (last_rval)) == REFERENCE_TYPE)
1729 last_rval = convert_from_reference (last_rval);
1731 else
1732 last_rval = decay_conversion (expr, complain);
1734 if (TYPE_PTR_P (TREE_TYPE (last_rval)))
1736 if (processing_template_decl)
1738 expr = build_min (ARROW_EXPR, TREE_TYPE (TREE_TYPE (last_rval)),
1739 orig_expr);
1740 TREE_SIDE_EFFECTS (expr) = TREE_SIDE_EFFECTS (last_rval);
1741 return expr;
1744 return cp_build_indirect_ref (last_rval, RO_NULL, complain);
1747 if (complain & tf_error)
1749 if (types_memoized)
1750 error ("result of %<operator->()%> yields non-pointer result");
1751 else
1752 error ("base operand of %<->%> is not a pointer");
1754 return error_mark_node;
1757 /* Return an expression for "DATUM .* COMPONENT". DATUM has not
1758 already been checked out to be of aggregate type. */
1760 tree
1761 build_m_component_ref (tree datum, tree component, tsubst_flags_t complain)
1763 tree ptrmem_type;
1764 tree objtype;
1765 tree type;
1766 tree binfo;
1767 tree ctype;
1769 if (error_operand_p (datum) || error_operand_p (component))
1770 return error_mark_node;
1772 datum = mark_lvalue_use (datum);
1773 component = mark_rvalue_use (component);
1775 ptrmem_type = TREE_TYPE (component);
1776 if (!TYPE_PTRMEM_P (ptrmem_type))
1778 if (complain & tf_error)
1779 error ("%qE cannot be used as a member pointer, since it is of "
1780 "type %qT", component, ptrmem_type);
1781 return error_mark_node;
1784 objtype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
1785 if (! MAYBE_CLASS_TYPE_P (objtype))
1787 if (complain & tf_error)
1788 error ("cannot apply member pointer %qE to %qE, which is of "
1789 "non-class type %qT", component, datum, objtype);
1790 return error_mark_node;
1793 type = TYPE_PTRMEM_POINTED_TO_TYPE (ptrmem_type);
1794 ctype = complete_type (TYPE_PTRMEM_CLASS_TYPE (ptrmem_type));
1796 if (!COMPLETE_TYPE_P (ctype))
1798 if (!same_type_p (ctype, objtype))
1799 goto mismatch;
1800 binfo = NULL;
1802 else
1804 binfo = lookup_base (objtype, ctype, ba_check, NULL, complain);
1806 if (!binfo)
1808 mismatch:
1809 if (complain & tf_error)
1810 error ("pointer to member type %qT incompatible with object "
1811 "type %qT", type, objtype);
1812 return error_mark_node;
1814 else if (binfo == error_mark_node)
1815 return error_mark_node;
1818 if (TYPE_PTRDATAMEM_P (ptrmem_type))
1820 cp_lvalue_kind kind = lvalue_kind (datum);
1821 tree ptype;
1823 /* Compute the type of the field, as described in [expr.ref].
1824 There's no such thing as a mutable pointer-to-member, so
1825 things are not as complex as they are for references to
1826 non-static data members. */
1827 type = cp_build_qualified_type (type,
1828 (cp_type_quals (type)
1829 | cp_type_quals (TREE_TYPE (datum))));
1831 datum = build_address (datum);
1833 /* Convert object to the correct base. */
1834 if (binfo)
1836 datum = build_base_path (PLUS_EXPR, datum, binfo, 1, complain);
1837 if (datum == error_mark_node)
1838 return error_mark_node;
1841 /* Build an expression for "object + offset" where offset is the
1842 value stored in the pointer-to-data-member. */
1843 ptype = build_pointer_type (type);
1844 datum = fold_build_pointer_plus (fold_convert (ptype, datum), component);
1845 datum = cp_build_indirect_ref (datum, RO_NULL, complain);
1846 if (datum == error_mark_node)
1847 return error_mark_node;
1849 /* If the object expression was an rvalue, return an rvalue. */
1850 if (kind & clk_class)
1851 datum = rvalue (datum);
1852 else if (kind & clk_rvalueref)
1853 datum = move (datum);
1854 return datum;
1856 else
1858 /* 5.5/6: In a .* expression whose object expression is an rvalue, the
1859 program is ill-formed if the second operand is a pointer to member
1860 function with ref-qualifier &. In a .* expression whose object
1861 expression is an lvalue, the program is ill-formed if the second
1862 operand is a pointer to member function with ref-qualifier &&. */
1863 if (FUNCTION_REF_QUALIFIED (type))
1865 bool lval = real_lvalue_p (datum);
1866 if (lval && FUNCTION_RVALUE_QUALIFIED (type))
1868 if (complain & tf_error)
1869 error ("pointer-to-member-function type %qT requires an rvalue",
1870 ptrmem_type);
1871 return error_mark_node;
1873 else if (!lval && !FUNCTION_RVALUE_QUALIFIED (type))
1875 if (complain & tf_error)
1876 error ("pointer-to-member-function type %qT requires an lvalue",
1877 ptrmem_type);
1878 return error_mark_node;
1881 return build2 (OFFSET_REF, type, datum, component);
1885 /* Return a tree node for the expression TYPENAME '(' PARMS ')'. */
1887 tree
1888 build_functional_cast (tree exp, tree parms, tsubst_flags_t complain)
1890 /* This is either a call to a constructor,
1891 or a C cast in C++'s `functional' notation. */
1893 /* The type to which we are casting. */
1894 tree type;
1895 vec<tree, va_gc> *parmvec;
1897 if (error_operand_p (exp) || parms == error_mark_node)
1898 return error_mark_node;
1900 if (TREE_CODE (exp) == TYPE_DECL)
1902 type = TREE_TYPE (exp);
1904 if (complain & tf_warning
1905 && TREE_DEPRECATED (type)
1906 && DECL_ARTIFICIAL (exp))
1907 warn_deprecated_use (type, NULL_TREE);
1909 else
1910 type = exp;
1912 /* We need to check this explicitly, since value-initialization of
1913 arrays is allowed in other situations. */
1914 if (TREE_CODE (type) == ARRAY_TYPE)
1916 if (complain & tf_error)
1917 error ("functional cast to array type %qT", type);
1918 return error_mark_node;
1921 if (type_uses_auto (type))
1923 if (complain & tf_error)
1924 error ("invalid use of %<auto%>");
1925 return error_mark_node;
1928 if (processing_template_decl)
1930 tree t;
1932 /* Diagnose this even in a template. We could also try harder
1933 to give all the usual errors when the type and args are
1934 non-dependent... */
1935 if (TREE_CODE (type) == REFERENCE_TYPE && !parms)
1937 if (complain & tf_error)
1938 error ("invalid value-initialization of reference type");
1939 return error_mark_node;
1942 t = build_min (CAST_EXPR, type, parms);
1943 /* We don't know if it will or will not have side effects. */
1944 TREE_SIDE_EFFECTS (t) = 1;
1945 return t;
1948 if (! MAYBE_CLASS_TYPE_P (type))
1950 if (parms == NULL_TREE)
1952 if (VOID_TYPE_P (type))
1953 return void_node;
1954 return build_value_init (cv_unqualified (type), complain);
1957 /* This must build a C cast. */
1958 parms = build_x_compound_expr_from_list (parms, ELK_FUNC_CAST, complain);
1959 return cp_build_c_cast (type, parms, complain);
1962 /* Prepare to evaluate as a call to a constructor. If this expression
1963 is actually used, for example,
1965 return X (arg1, arg2, ...);
1967 then the slot being initialized will be filled in. */
1969 if (!complete_type_or_maybe_complain (type, NULL_TREE, complain))
1970 return error_mark_node;
1971 if (abstract_virtuals_error_sfinae (ACU_CAST, type, complain))
1972 return error_mark_node;
1974 /* [expr.type.conv]
1976 If the expression list is a single-expression, the type
1977 conversion is equivalent (in definedness, and if defined in
1978 meaning) to the corresponding cast expression. */
1979 if (parms && TREE_CHAIN (parms) == NULL_TREE)
1980 return cp_build_c_cast (type, TREE_VALUE (parms), complain);
1982 /* [expr.type.conv]
1984 The expression T(), where T is a simple-type-specifier for a
1985 non-array complete object type or the (possibly cv-qualified)
1986 void type, creates an rvalue of the specified type, which is
1987 value-initialized. */
1989 if (parms == NULL_TREE)
1991 exp = build_value_init (type, complain);
1992 exp = get_target_expr_sfinae (exp, complain);
1993 return exp;
1996 /* Call the constructor. */
1997 parmvec = make_tree_vector ();
1998 for (; parms != NULL_TREE; parms = TREE_CHAIN (parms))
1999 vec_safe_push (parmvec, TREE_VALUE (parms));
2000 exp = build_special_member_call (NULL_TREE, complete_ctor_identifier,
2001 &parmvec, type, LOOKUP_NORMAL, complain);
2002 release_tree_vector (parmvec);
2004 if (exp == error_mark_node)
2005 return error_mark_node;
2007 return build_cplus_new (type, exp, complain);
2011 /* Add new exception specifier SPEC, to the LIST we currently have.
2012 If it's already in LIST then do nothing.
2013 Moan if it's bad and we're allowed to. COMPLAIN < 0 means we
2014 know what we're doing. */
2016 tree
2017 add_exception_specifier (tree list, tree spec, int complain)
2019 bool ok;
2020 tree core = spec;
2021 bool is_ptr;
2022 diagnostic_t diag_type = DK_UNSPECIFIED; /* none */
2024 if (spec == error_mark_node)
2025 return list;
2027 gcc_assert (spec && (!list || TREE_VALUE (list)));
2029 /* [except.spec] 1, type in an exception specifier shall not be
2030 incomplete, or pointer or ref to incomplete other than pointer
2031 to cv void. */
2032 is_ptr = TYPE_PTR_P (core);
2033 if (is_ptr || TREE_CODE (core) == REFERENCE_TYPE)
2034 core = TREE_TYPE (core);
2035 if (complain < 0)
2036 ok = true;
2037 else if (VOID_TYPE_P (core))
2038 ok = is_ptr;
2039 else if (TREE_CODE (core) == TEMPLATE_TYPE_PARM)
2040 ok = true;
2041 else if (processing_template_decl)
2042 ok = true;
2043 else
2045 ok = true;
2046 /* 15.4/1 says that types in an exception specifier must be complete,
2047 but it seems more reasonable to only require this on definitions
2048 and calls. So just give a pedwarn at this point; we will give an
2049 error later if we hit one of those two cases. */
2050 if (!COMPLETE_TYPE_P (complete_type (core)))
2051 diag_type = DK_PEDWARN; /* pedwarn */
2054 if (ok)
2056 tree probe;
2058 for (probe = list; probe; probe = TREE_CHAIN (probe))
2059 if (same_type_p (TREE_VALUE (probe), spec))
2060 break;
2061 if (!probe)
2062 list = tree_cons (NULL_TREE, spec, list);
2064 else
2065 diag_type = DK_ERROR; /* error */
2067 if (diag_type != DK_UNSPECIFIED
2068 && (complain & tf_warning_or_error))
2069 cxx_incomplete_type_diagnostic (NULL_TREE, core, diag_type);
2071 return list;
2074 /* Like nothrow_spec_p, but don't abort on deferred noexcept. */
2076 static bool
2077 nothrow_spec_p_uninst (const_tree spec)
2079 if (DEFERRED_NOEXCEPT_SPEC_P (spec))
2080 return false;
2081 return nothrow_spec_p (spec);
2084 /* Combine the two exceptions specifier lists LIST and ADD, and return
2085 their union. */
2087 tree
2088 merge_exception_specifiers (tree list, tree add)
2090 tree noex, orig_list;
2092 /* No exception-specifier or noexcept(false) are less strict than
2093 anything else. Prefer the newer variant (LIST). */
2094 if (!list || list == noexcept_false_spec)
2095 return list;
2096 else if (!add || add == noexcept_false_spec)
2097 return add;
2099 /* noexcept(true) and throw() are stricter than anything else.
2100 As above, prefer the more recent one (LIST). */
2101 if (nothrow_spec_p_uninst (add))
2102 return list;
2104 /* Two implicit noexcept specs (e.g. on a destructor) are equivalent. */
2105 if (UNEVALUATED_NOEXCEPT_SPEC_P (add)
2106 && UNEVALUATED_NOEXCEPT_SPEC_P (list))
2107 return list;
2108 /* We should have instantiated other deferred noexcept specs by now. */
2109 gcc_assert (!DEFERRED_NOEXCEPT_SPEC_P (add));
2111 if (nothrow_spec_p_uninst (list))
2112 return add;
2113 noex = TREE_PURPOSE (list);
2114 gcc_checking_assert (!TREE_PURPOSE (add)
2115 || cp_tree_equal (noex, TREE_PURPOSE (add)));
2117 /* Combine the dynamic-exception-specifiers, if any. */
2118 orig_list = list;
2119 for (; add && TREE_VALUE (add); add = TREE_CHAIN (add))
2121 tree spec = TREE_VALUE (add);
2122 tree probe;
2124 for (probe = orig_list; probe && TREE_VALUE (probe);
2125 probe = TREE_CHAIN (probe))
2126 if (same_type_p (TREE_VALUE (probe), spec))
2127 break;
2128 if (!probe)
2130 spec = build_tree_list (NULL_TREE, spec);
2131 TREE_CHAIN (spec) = list;
2132 list = spec;
2136 /* Keep the noexcept-specifier at the beginning of the list. */
2137 if (noex != TREE_PURPOSE (list))
2138 list = tree_cons (noex, TREE_VALUE (list), TREE_CHAIN (list));
2140 return list;
2143 /* Subroutine of build_call. Ensure that each of the types in the
2144 exception specification is complete. Technically, 15.4/1 says that
2145 they need to be complete when we see a declaration of the function,
2146 but we should be able to get away with only requiring this when the
2147 function is defined or called. See also add_exception_specifier. */
2149 void
2150 require_complete_eh_spec_types (tree fntype, tree decl)
2152 tree raises;
2153 /* Don't complain about calls to op new. */
2154 if (decl && DECL_ARTIFICIAL (decl))
2155 return;
2156 for (raises = TYPE_RAISES_EXCEPTIONS (fntype); raises;
2157 raises = TREE_CHAIN (raises))
2159 tree type = TREE_VALUE (raises);
2160 if (type && !COMPLETE_TYPE_P (type))
2162 if (decl)
2163 error
2164 ("call to function %qD which throws incomplete type %q#T",
2165 decl, type);
2166 else
2167 error ("call to function which throws incomplete type %q#T",
2168 decl);
2174 #include "gt-cp-typeck2.h"