* gcc.dg/atomic-compare-exchange-1.c,
[official-gcc.git] / gcc / cp / typeck2.c
blob9da8e3de8444f8533470c0c2f6f63ffacad8326c
1 /* Report error messages, build initializers, and perform
2 some front-end optimizations for C++ compiler.
3 Copyright (C) 1987-2013 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 "intl.h"
34 #include "cp-tree.h"
35 #include "flags.h"
36 #include "diagnostic-core.h"
38 static tree
39 process_init_constructor (tree type, tree init, tsubst_flags_t complain);
42 /* Print an error message stemming from an attempt to use
43 BASETYPE as a base class for TYPE. */
45 tree
46 error_not_base_type (tree basetype, tree type)
48 if (TREE_CODE (basetype) == FUNCTION_DECL)
49 basetype = DECL_CONTEXT (basetype);
50 error ("type %qT is not a base type for type %qT", basetype, type);
51 return error_mark_node;
54 tree
55 binfo_or_else (tree base, tree type)
57 tree binfo = lookup_base (type, base, ba_unique,
58 NULL, tf_warning_or_error);
60 if (binfo == error_mark_node)
61 return NULL_TREE;
62 else if (!binfo)
63 error_not_base_type (base, type);
64 return binfo;
67 /* According to ARM $7.1.6, "A `const' object may be initialized, but its
68 value may not be changed thereafter. */
70 void
71 cxx_readonly_error (tree arg, enum lvalue_use errstring)
74 /* This macro is used to emit diagnostics to ensure that all format
75 strings are complete sentences, visible to gettext and checked at
76 compile time. */
78 #define ERROR_FOR_ASSIGNMENT(AS, ASM, IN, DE, ARG) \
79 do { \
80 switch (errstring) \
81 { \
82 case lv_assign: \
83 error(AS, ARG); \
84 break; \
85 case lv_asm: \
86 error(ASM, ARG); \
87 break; \
88 case lv_increment: \
89 error (IN, ARG); \
90 break; \
91 case lv_decrement: \
92 error (DE, ARG); \
93 break; \
94 default: \
95 gcc_unreachable (); \
96 } \
97 } while (0)
99 /* Handle C++-specific things first. */
101 if (VAR_P (arg)
102 && DECL_LANG_SPECIFIC (arg)
103 && DECL_IN_AGGR_P (arg)
104 && !TREE_STATIC (arg))
105 ERROR_FOR_ASSIGNMENT (G_("assignment of "
106 "constant field %qD"),
107 G_("constant field %qD "
108 "used as %<asm%> output"),
109 G_("increment of "
110 "constant field %qD"),
111 G_("decrement of "
112 "constant field %qD"),
113 arg);
114 else if (INDIRECT_REF_P (arg)
115 && TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 0))) == REFERENCE_TYPE
116 && (VAR_P (TREE_OPERAND (arg, 0))
117 || TREE_CODE (TREE_OPERAND (arg, 0)) == PARM_DECL))
118 ERROR_FOR_ASSIGNMENT (G_("assignment of "
119 "read-only reference %qD"),
120 G_("read-only reference %qD "
121 "used as %<asm%> output"),
122 G_("increment of "
123 "read-only reference %qD"),
124 G_("decrement of "
125 "read-only reference %qD"),
126 TREE_OPERAND (arg, 0));
127 else
128 readonly_error (arg, errstring);
132 /* Structure that holds information about declarations whose type was
133 incomplete and we could not check whether it was abstract or not. */
135 struct GTY((chain_next ("%h.next"))) pending_abstract_type {
136 /* Declaration which we are checking for abstractness. It is either
137 a DECL node, or an IDENTIFIER_NODE if we do not have a full
138 declaration available. */
139 tree decl;
141 /* Type which will be checked for abstractness. */
142 tree type;
144 /* Kind of use in an unnamed declarator. */
145 abstract_class_use use;
147 /* Position of the declaration. This is only needed for IDENTIFIER_NODEs,
148 because DECLs already carry locus information. */
149 location_t locus;
151 /* Link to the next element in list. */
152 struct pending_abstract_type* next;
156 /* Compute the hash value of the node VAL. This function is used by the
157 hash table abstract_pending_vars. */
159 static hashval_t
160 pat_calc_hash (const void* val)
162 const struct pending_abstract_type *pat =
163 (const struct pending_abstract_type *) val;
164 return (hashval_t) TYPE_UID (pat->type);
168 /* Compare node VAL1 with the type VAL2. This function is used by the
169 hash table abstract_pending_vars. */
171 static int
172 pat_compare (const void* val1, const void* val2)
174 const struct pending_abstract_type *const pat1 =
175 (const struct pending_abstract_type *) val1;
176 const_tree const type2 = (const_tree)val2;
178 return (pat1->type == type2);
181 /* Hash table that maintains pending_abstract_type nodes, for which we still
182 need to check for type abstractness. The key of the table is the type
183 of the declaration. */
184 static GTY ((param_is (struct pending_abstract_type)))
185 htab_t abstract_pending_vars = NULL;
187 static int abstract_virtuals_error_sfinae (tree, tree, abstract_class_use, tsubst_flags_t);
189 /* This function is called after TYPE is completed, and will check if there
190 are pending declarations for which we still need to verify the abstractness
191 of TYPE, and emit a diagnostic (through abstract_virtuals_error) if TYPE
192 turned out to be incomplete. */
194 void
195 complete_type_check_abstract (tree type)
197 void **slot;
198 struct pending_abstract_type *pat;
199 location_t cur_loc = input_location;
201 gcc_assert (COMPLETE_TYPE_P (type));
203 if (!abstract_pending_vars)
204 return;
206 /* Retrieve the list of pending declarations for this type. */
207 slot = htab_find_slot_with_hash (abstract_pending_vars, type,
208 (hashval_t)TYPE_UID (type), NO_INSERT);
209 if (!slot)
210 return;
211 pat = (struct pending_abstract_type*)*slot;
212 gcc_assert (pat);
214 /* If the type is not abstract, do not do anything. */
215 if (CLASSTYPE_PURE_VIRTUALS (type))
217 struct pending_abstract_type *prev = 0, *next;
219 /* Reverse the list to emit the errors in top-down order. */
220 for (; pat; pat = next)
222 next = pat->next;
223 pat->next = prev;
224 prev = pat;
226 pat = prev;
228 /* Go through the list, and call abstract_virtuals_error for each
229 element: it will issue a diagnostic if the type is abstract. */
230 while (pat)
232 gcc_assert (type == pat->type);
234 /* Tweak input_location so that the diagnostic appears at the correct
235 location. Notice that this is only needed if the decl is an
236 IDENTIFIER_NODE. */
237 input_location = pat->locus;
238 abstract_virtuals_error_sfinae (pat->decl, pat->type, pat->use,
239 tf_warning_or_error);
240 pat = pat->next;
244 htab_clear_slot (abstract_pending_vars, slot);
246 input_location = cur_loc;
250 /* If TYPE has abstract virtual functions, issue an error about trying
251 to create an object of that type. DECL is the object declared, or
252 NULL_TREE if the declaration is unavailable, in which case USE specifies
253 the kind of invalid use. Returns 1 if an error occurred; zero if
254 all was well. */
256 static int
257 abstract_virtuals_error_sfinae (tree decl, tree type, abstract_class_use use,
258 tsubst_flags_t complain)
260 vec<tree, va_gc> *pure;
262 /* This function applies only to classes. Any other entity can never
263 be abstract. */
264 if (!CLASS_TYPE_P (type))
265 return 0;
266 type = TYPE_MAIN_VARIANT (type);
268 #if 0
269 /* Instantiation here seems to be required by the standard,
270 but breaks e.g. boost::bind. FIXME! */
271 /* In SFINAE, non-N3276 context, force instantiation. */
272 if (!(complain & (tf_error|tf_decltype)))
273 complete_type (type);
274 #endif
276 /* If the type is incomplete, we register it within a hash table,
277 so that we can check again once it is completed. This makes sense
278 only for objects for which we have a declaration or at least a
279 name. */
280 if (!COMPLETE_TYPE_P (type) && (complain & tf_error))
282 void **slot;
283 struct pending_abstract_type *pat;
285 gcc_assert (!decl || DECL_P (decl) || identifier_p (decl));
287 if (!abstract_pending_vars)
288 abstract_pending_vars = htab_create_ggc (31, &pat_calc_hash,
289 &pat_compare, NULL);
291 slot = htab_find_slot_with_hash (abstract_pending_vars, type,
292 (hashval_t)TYPE_UID (type), INSERT);
294 pat = ggc_alloc_pending_abstract_type ();
295 pat->type = type;
296 pat->decl = decl;
297 pat->use = use;
298 pat->locus = ((decl && DECL_P (decl))
299 ? DECL_SOURCE_LOCATION (decl)
300 : input_location);
302 pat->next = (struct pending_abstract_type *) *slot;
303 *slot = pat;
305 return 0;
308 if (!TYPE_SIZE (type))
309 /* TYPE is being defined, and during that time
310 CLASSTYPE_PURE_VIRTUALS holds the inline friends. */
311 return 0;
313 pure = CLASSTYPE_PURE_VIRTUALS (type);
314 if (!pure)
315 return 0;
317 if (!(complain & tf_error))
318 return 1;
320 if (decl)
322 if (VAR_P (decl))
323 error ("cannot declare variable %q+D to be of abstract "
324 "type %qT", decl, type);
325 else if (TREE_CODE (decl) == PARM_DECL)
327 if (DECL_NAME (decl))
328 error ("cannot declare parameter %q+D to be of abstract type %qT",
329 decl, type);
330 else
331 error ("cannot declare parameter to be of abstract type %qT",
332 type);
334 else if (TREE_CODE (decl) == FIELD_DECL)
335 error ("cannot declare field %q+D to be of abstract type %qT",
336 decl, type);
337 else if (TREE_CODE (decl) == FUNCTION_DECL
338 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
339 error ("invalid abstract return type for member function %q+#D", decl);
340 else if (TREE_CODE (decl) == FUNCTION_DECL)
341 error ("invalid abstract return type for function %q+#D", decl);
342 else if (identifier_p (decl))
343 /* Here we do not have location information. */
344 error ("invalid abstract type %qT for %qE", type, decl);
345 else
346 error ("invalid abstract type for %q+D", decl);
348 else switch (use)
350 case ACU_ARRAY:
351 error ("creating array of %qT, which is an abstract class type", type);
352 break;
353 case ACU_CAST:
354 error ("invalid cast to abstract class type %qT", type);
355 break;
356 case ACU_NEW:
357 error ("invalid new-expression of abstract class type %qT", type);
358 break;
359 case ACU_RETURN:
360 error ("invalid abstract return type %qT", type);
361 break;
362 case ACU_PARM:
363 error ("invalid abstract parameter type %qT", type);
364 break;
365 case ACU_THROW:
366 error ("expression of abstract class type %qT cannot "
367 "be used in throw-expression", type);
368 break;
369 case ACU_CATCH:
370 error ("cannot declare catch parameter to be of abstract "
371 "class type %qT", type);
372 break;
373 default:
374 error ("cannot allocate an object of abstract type %qT", type);
377 /* Only go through this once. */
378 if (pure->length ())
380 unsigned ix;
381 tree fn;
383 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
384 " because the following virtual functions are pure within %qT:",
385 type);
387 FOR_EACH_VEC_ELT (*pure, ix, fn)
388 if (! DECL_CLONED_FUNCTION_P (fn)
389 || DECL_COMPLETE_DESTRUCTOR_P (fn))
390 inform (input_location, "\t%+#D", fn);
392 /* Now truncate the vector. This leaves it non-null, so we know
393 there are pure virtuals, but empty so we don't list them out
394 again. */
395 pure->truncate (0);
398 return 1;
402 abstract_virtuals_error_sfinae (tree decl, tree type, tsubst_flags_t complain)
404 return abstract_virtuals_error_sfinae (decl, type, ACU_UNKNOWN, complain);
408 abstract_virtuals_error_sfinae (abstract_class_use use, tree type,
409 tsubst_flags_t complain)
411 return abstract_virtuals_error_sfinae (NULL_TREE, type, use, complain);
415 /* Wrapper for the above function in the common case of wanting errors. */
418 abstract_virtuals_error (tree decl, tree type)
420 return abstract_virtuals_error_sfinae (decl, type, tf_warning_or_error);
424 abstract_virtuals_error (abstract_class_use use, tree type)
426 return abstract_virtuals_error_sfinae (use, type, tf_warning_or_error);
429 /* Print an error message for invalid use of an incomplete type.
430 VALUE is the expression that was used (or 0 if that isn't known)
431 and TYPE is the type that was invalid. DIAG_KIND indicates the
432 type of diagnostic (see diagnostic.def). */
434 void
435 cxx_incomplete_type_diagnostic (const_tree value, const_tree type,
436 diagnostic_t diag_kind)
438 int decl = 0;
440 gcc_assert (diag_kind == DK_WARNING
441 || diag_kind == DK_PEDWARN
442 || diag_kind == DK_ERROR);
444 /* Avoid duplicate error message. */
445 if (TREE_CODE (type) == ERROR_MARK)
446 return;
448 if (value != 0 && (VAR_P (value)
449 || TREE_CODE (value) == PARM_DECL
450 || TREE_CODE (value) == FIELD_DECL))
452 emit_diagnostic (diag_kind, input_location, 0,
453 "%q+D has incomplete type", value);
454 decl = 1;
456 retry:
457 /* We must print an error message. Be clever about what it says. */
459 switch (TREE_CODE (type))
461 case RECORD_TYPE:
462 case UNION_TYPE:
463 case ENUMERAL_TYPE:
464 if (!decl)
465 emit_diagnostic (diag_kind, input_location, 0,
466 "invalid use of incomplete type %q#T", type);
467 if (!TYPE_TEMPLATE_INFO (type))
468 emit_diagnostic (diag_kind, input_location, 0,
469 "forward declaration of %q+#T", type);
470 else
471 emit_diagnostic (diag_kind, input_location, 0,
472 "declaration of %q+#T", type);
473 break;
475 case VOID_TYPE:
476 emit_diagnostic (diag_kind, input_location, 0,
477 "invalid use of %qT", type);
478 break;
480 case ARRAY_TYPE:
481 if (TYPE_DOMAIN (type))
483 type = TREE_TYPE (type);
484 goto retry;
486 emit_diagnostic (diag_kind, input_location, 0,
487 "invalid use of array with unspecified bounds");
488 break;
490 case OFFSET_TYPE:
491 bad_member:
493 tree member = TREE_OPERAND (value, 1);
494 if (is_overloaded_fn (member))
495 member = get_first_fn (member);
496 if (DECL_FUNCTION_MEMBER_P (member)
497 && ! flag_ms_extensions)
498 emit_diagnostic (diag_kind, input_location, 0,
499 "invalid use of member function "
500 "(did you forget the %<()%> ?)");
501 else
502 emit_diagnostic (diag_kind, input_location, 0,
503 "invalid use of member "
504 "(did you forget the %<&%> ?)");
506 break;
508 case TEMPLATE_TYPE_PARM:
509 if (is_auto (type))
510 emit_diagnostic (diag_kind, input_location, 0,
511 "invalid use of %<auto%>");
512 else
513 emit_diagnostic (diag_kind, input_location, 0,
514 "invalid use of template type parameter %qT", type);
515 break;
517 case BOUND_TEMPLATE_TEMPLATE_PARM:
518 emit_diagnostic (diag_kind, input_location, 0,
519 "invalid use of template template parameter %qT",
520 TYPE_NAME (type));
521 break;
523 case TYPENAME_TYPE:
524 emit_diagnostic (diag_kind, input_location, 0,
525 "invalid use of dependent type %qT", type);
526 break;
528 case LANG_TYPE:
529 if (type == init_list_type_node)
531 emit_diagnostic (diag_kind, input_location, 0,
532 "invalid use of brace-enclosed initializer list");
533 break;
535 gcc_assert (type == unknown_type_node);
536 if (value && TREE_CODE (value) == COMPONENT_REF)
537 goto bad_member;
538 else if (value && TREE_CODE (value) == ADDR_EXPR)
539 emit_diagnostic (diag_kind, input_location, 0,
540 "address of overloaded function with no contextual "
541 "type information");
542 else if (value && TREE_CODE (value) == OVERLOAD)
543 emit_diagnostic (diag_kind, input_location, 0,
544 "overloaded function with no contextual type information");
545 else
546 emit_diagnostic (diag_kind, input_location, 0,
547 "insufficient contextual information to determine type");
548 break;
550 default:
551 gcc_unreachable ();
555 /* Backward-compatibility interface to incomplete_type_diagnostic;
556 required by ../tree.c. */
557 #undef cxx_incomplete_type_error
558 void
559 cxx_incomplete_type_error (const_tree value, const_tree type)
561 cxx_incomplete_type_diagnostic (value, type, DK_ERROR);
565 /* The recursive part of split_nonconstant_init. DEST is an lvalue
566 expression to which INIT should be assigned. INIT is a CONSTRUCTOR.
567 Return true if the whole of the value was initialized by the
568 generated statements. */
570 static bool
571 split_nonconstant_init_1 (tree dest, tree init)
573 unsigned HOST_WIDE_INT idx;
574 tree field_index, value;
575 tree type = TREE_TYPE (dest);
576 tree inner_type = NULL;
577 bool array_type_p = false;
578 bool complete_p = true;
579 HOST_WIDE_INT num_split_elts = 0;
581 switch (TREE_CODE (type))
583 case ARRAY_TYPE:
584 inner_type = TREE_TYPE (type);
585 array_type_p = true;
586 /* FALLTHRU */
588 case RECORD_TYPE:
589 case UNION_TYPE:
590 case QUAL_UNION_TYPE:
591 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), idx,
592 field_index, value)
594 /* The current implementation of this algorithm assumes that
595 the field was set for all the elements. This is usually done
596 by process_init_constructor. */
597 gcc_assert (field_index);
599 if (!array_type_p)
600 inner_type = TREE_TYPE (field_index);
602 if (TREE_CODE (value) == CONSTRUCTOR)
604 tree sub;
606 if (array_type_p)
607 sub = build4 (ARRAY_REF, inner_type, dest, field_index,
608 NULL_TREE, NULL_TREE);
609 else
610 sub = build3 (COMPONENT_REF, inner_type, dest, field_index,
611 NULL_TREE);
613 if (!split_nonconstant_init_1 (sub, value))
614 complete_p = false;
615 num_split_elts++;
617 else if (!initializer_constant_valid_p (value, inner_type))
619 tree code;
620 tree sub;
622 /* FIXME: Ordered removal is O(1) so the whole function is
623 worst-case quadratic. This could be fixed using an aside
624 bitmap to record which elements must be removed and remove
625 them all at the same time. Or by merging
626 split_non_constant_init into process_init_constructor_array,
627 that is separating constants from non-constants while building
628 the vector. */
629 CONSTRUCTOR_ELTS (init)->ordered_remove (idx);
630 --idx;
632 if (array_type_p)
633 sub = build4 (ARRAY_REF, inner_type, dest, field_index,
634 NULL_TREE, NULL_TREE);
635 else
636 sub = build3 (COMPONENT_REF, inner_type, dest, field_index,
637 NULL_TREE);
639 code = build2 (INIT_EXPR, inner_type, sub, value);
640 code = build_stmt (input_location, EXPR_STMT, code);
641 code = maybe_cleanup_point_expr_void (code);
642 add_stmt (code);
643 if (type_build_dtor_call (inner_type))
645 code = (build_special_member_call
646 (sub, complete_dtor_identifier, NULL, inner_type,
647 LOOKUP_NORMAL, tf_warning_or_error));
648 if (!TYPE_HAS_TRIVIAL_DESTRUCTOR (inner_type))
649 finish_eh_cleanup (code);
652 num_split_elts++;
655 break;
657 case VECTOR_TYPE:
658 if (!initializer_constant_valid_p (init, type))
660 tree code;
661 tree cons = copy_node (init);
662 CONSTRUCTOR_ELTS (init) = NULL;
663 code = build2 (MODIFY_EXPR, type, dest, cons);
664 code = build_stmt (input_location, EXPR_STMT, code);
665 add_stmt (code);
666 num_split_elts += CONSTRUCTOR_NELTS (init);
668 break;
670 default:
671 gcc_unreachable ();
674 /* The rest of the initializer is now a constant. */
675 TREE_CONSTANT (init) = 1;
676 return complete_p && complete_ctor_at_level_p (TREE_TYPE (init),
677 num_split_elts, inner_type);
680 /* A subroutine of store_init_value. Splits non-constant static
681 initializer INIT into a constant part and generates code to
682 perform the non-constant part of the initialization to DEST.
683 Returns the code for the runtime init. */
685 static tree
686 split_nonconstant_init (tree dest, tree init)
688 tree code;
690 if (TREE_CODE (init) == CONSTRUCTOR)
692 code = push_stmt_list ();
693 if (split_nonconstant_init_1 (dest, init))
694 init = NULL_TREE;
695 code = pop_stmt_list (code);
696 DECL_INITIAL (dest) = init;
697 TREE_READONLY (dest) = 0;
699 else
700 code = build2 (INIT_EXPR, TREE_TYPE (dest), dest, init);
702 return code;
705 /* Perform appropriate conversions on the initial value of a variable,
706 store it in the declaration DECL,
707 and print any error messages that are appropriate.
708 If the init is invalid, store an ERROR_MARK.
710 C++: Note that INIT might be a TREE_LIST, which would mean that it is
711 a base class initializer for some aggregate type, hopefully compatible
712 with DECL. If INIT is a single element, and DECL is an aggregate
713 type, we silently convert INIT into a TREE_LIST, allowing a constructor
714 to be called.
716 If INIT is a TREE_LIST and there is no constructor, turn INIT
717 into a CONSTRUCTOR and use standard initialization techniques.
718 Perhaps a warning should be generated?
720 Returns code to be executed if initialization could not be performed
721 for static variable. In that case, caller must emit the code. */
723 tree
724 store_init_value (tree decl, tree init, vec<tree, va_gc>** cleanups, int flags)
726 tree value, type;
728 /* If variable's type was invalidly declared, just ignore it. */
730 type = TREE_TYPE (decl);
731 if (TREE_CODE (type) == ERROR_MARK)
732 return NULL_TREE;
734 if (MAYBE_CLASS_TYPE_P (type))
736 if (TREE_CODE (init) == TREE_LIST)
738 error ("constructor syntax used, but no constructor declared "
739 "for type %qT", type);
740 init = build_constructor_from_list (init_list_type_node, nreverse (init));
743 else if (TREE_CODE (init) == TREE_LIST
744 && TREE_TYPE (init) != unknown_type_node)
746 gcc_assert (TREE_CODE (decl) != RESULT_DECL);
748 if (TREE_CODE (init) == TREE_LIST
749 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
751 error ("cannot initialize arrays using this syntax");
752 return NULL_TREE;
754 else
755 /* We get here with code like `int a (2);' */
756 init = build_x_compound_expr_from_list (init, ELK_INIT,
757 tf_warning_or_error);
760 /* End of special C++ code. */
762 if (flags & LOOKUP_ALREADY_DIGESTED)
763 value = init;
764 else
765 /* Digest the specified initializer into an expression. */
766 value = digest_init_flags (type, init, flags);
768 value = extend_ref_init_temps (decl, value, cleanups);
770 /* In C++0x constant expression is a semantic, not syntactic, property.
771 In C++98, make sure that what we thought was a constant expression at
772 template definition time is still constant and otherwise perform this
773 as optimization, e.g. to fold SIZEOF_EXPRs in the initializer. */
774 if (decl_maybe_constant_var_p (decl) || TREE_STATIC (decl))
776 bool const_init;
777 value = fold_non_dependent_expr (value);
778 if (DECL_DECLARED_CONSTEXPR_P (decl)
779 || DECL_IN_AGGR_P (decl))
781 /* Diagnose a non-constant initializer for constexpr. */
782 if (processing_template_decl
783 && !require_potential_constant_expression (value))
784 value = error_mark_node;
785 else
786 value = cxx_constant_value (value);
788 value = maybe_constant_init (value);
789 const_init = (reduced_constant_expression_p (value)
790 || error_operand_p (value));
791 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = const_init;
792 TREE_CONSTANT (decl) = const_init && decl_maybe_constant_var_p (decl);
795 /* If the initializer is not a constant, fill in DECL_INITIAL with
796 the bits that are constant, and then return an expression that
797 will perform the dynamic initialization. */
798 if (value != error_mark_node
799 && (TREE_SIDE_EFFECTS (value)
800 || array_of_runtime_bound_p (type)
801 || ! reduced_constant_expression_p (value)))
803 if (TREE_CODE (type) == ARRAY_TYPE
804 && (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (type))
805 || array_of_runtime_bound_p (type)))
806 /* For an array, we only need/want a single cleanup region rather
807 than one per element. */
808 return build_vec_init (decl, NULL_TREE, value, false, 1,
809 tf_warning_or_error);
810 else
811 return split_nonconstant_init (decl, value);
813 /* If the value is a constant, just put it in DECL_INITIAL. If DECL
814 is an automatic variable, the middle end will turn this into a
815 dynamic initialization later. */
816 DECL_INITIAL (decl) = value;
817 return NULL_TREE;
821 /* Give errors about narrowing conversions within { }. */
823 void
824 check_narrowing (tree type, tree init)
826 tree ftype = unlowered_expr_type (init);
827 bool ok = true;
828 REAL_VALUE_TYPE d;
830 if (!warn_narrowing || !ARITHMETIC_TYPE_P (type))
831 return;
833 if (BRACE_ENCLOSED_INITIALIZER_P (init)
834 && TREE_CODE (type) == COMPLEX_TYPE)
836 tree elttype = TREE_TYPE (type);
837 if (CONSTRUCTOR_NELTS (init) > 0)
838 check_narrowing (elttype, CONSTRUCTOR_ELT (init, 0)->value);
839 if (CONSTRUCTOR_NELTS (init) > 1)
840 check_narrowing (elttype, CONSTRUCTOR_ELT (init, 1)->value);
841 return;
844 init = maybe_constant_value (init);
846 if (TREE_CODE (type) == INTEGER_TYPE
847 && TREE_CODE (ftype) == REAL_TYPE)
848 ok = false;
849 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype)
850 && CP_INTEGRAL_TYPE_P (type))
852 if (TREE_CODE (ftype) == ENUMERAL_TYPE)
853 /* Check for narrowing based on the values of the enumeration. */
854 ftype = ENUM_UNDERLYING_TYPE (ftype);
855 if ((tree_int_cst_lt (TYPE_MAX_VALUE (type),
856 TYPE_MAX_VALUE (ftype))
857 || tree_int_cst_lt (TYPE_MIN_VALUE (ftype),
858 TYPE_MIN_VALUE (type)))
859 && (TREE_CODE (init) != INTEGER_CST
860 || !int_fits_type_p (init, type)))
861 ok = false;
863 else if (TREE_CODE (ftype) == REAL_TYPE
864 && TREE_CODE (type) == REAL_TYPE)
866 if (TYPE_PRECISION (type) < TYPE_PRECISION (ftype))
868 if (TREE_CODE (init) == REAL_CST)
870 /* Issue 703: Loss of precision is OK as long as the value is
871 within the representable range of the new type. */
872 REAL_VALUE_TYPE r;
873 d = TREE_REAL_CST (init);
874 real_convert (&r, TYPE_MODE (type), &d);
875 if (real_isinf (&r))
876 ok = false;
878 else
879 ok = false;
882 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype)
883 && TREE_CODE (type) == REAL_TYPE)
885 ok = false;
886 if (TREE_CODE (init) == INTEGER_CST)
888 d = real_value_from_int_cst (0, init);
889 if (exact_real_truncate (TYPE_MODE (type), &d))
890 ok = true;
894 if (!ok)
896 if (cxx_dialect >= cxx11)
897 pedwarn (EXPR_LOC_OR_HERE (init), OPT_Wnarrowing,
898 "narrowing conversion of %qE from %qT to %qT inside { }",
899 init, ftype, type);
900 else
901 warning_at (EXPR_LOC_OR_HERE (init), OPT_Wnarrowing,
902 "narrowing conversion of %qE from %qT to %qT inside { } "
903 "is ill-formed in C++11", init, ftype, type);
907 /* Process the initializer INIT for a variable of type TYPE, emitting
908 diagnostics for invalid initializers and converting the initializer as
909 appropriate.
911 For aggregate types, it assumes that reshape_init has already run, thus the
912 initializer will have the right shape (brace elision has been undone).
914 NESTED is true iff we are being called for an element of a CONSTRUCTOR. */
916 static tree
917 digest_init_r (tree type, tree init, bool nested, int flags,
918 tsubst_flags_t complain)
920 enum tree_code code = TREE_CODE (type);
922 if (error_operand_p (init))
923 return error_mark_node;
925 gcc_assert (init);
927 /* We must strip the outermost array type when completing the type,
928 because the its bounds might be incomplete at the moment. */
929 if (!complete_type_or_maybe_complain (TREE_CODE (type) == ARRAY_TYPE
930 ? TREE_TYPE (type) : type, NULL_TREE,
931 complain))
932 return error_mark_node;
934 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue
935 (g++.old-deja/g++.law/casts2.C). */
936 if (TREE_CODE (init) == NON_LVALUE_EXPR)
937 init = TREE_OPERAND (init, 0);
939 /* Initialization of an array of chars from a string constant. The initializer
940 can be optionally enclosed in braces, but reshape_init has already removed
941 them if they were present. */
942 if (code == ARRAY_TYPE)
944 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
945 if (char_type_p (typ1)
946 /*&& init */
947 && TREE_CODE (init) == STRING_CST)
949 tree char_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (init)));
951 if (TYPE_PRECISION (typ1) == BITS_PER_UNIT)
953 if (char_type != char_type_node)
955 if (complain & tf_error)
956 error ("char-array initialized from wide string");
957 return error_mark_node;
960 else
962 if (char_type == char_type_node)
964 if (complain & tf_error)
965 error ("int-array initialized from non-wide string");
966 return error_mark_node;
968 else if (char_type != typ1)
970 if (complain & tf_error)
971 error ("int-array initialized from incompatible "
972 "wide string");
973 return error_mark_node;
977 if (type != TREE_TYPE (init))
979 init = copy_node (init);
980 TREE_TYPE (init) = type;
982 if (TYPE_DOMAIN (type) != 0 && TREE_CONSTANT (TYPE_SIZE (type)))
984 int size = TREE_INT_CST_LOW (TYPE_SIZE (type));
985 size = (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
986 /* In C it is ok to subtract 1 from the length of the string
987 because it's ok to ignore the terminating null char that is
988 counted in the length of the constant, but in C++ this would
989 be invalid. */
990 if (size < TREE_STRING_LENGTH (init))
991 permerror (input_location, "initializer-string for array "
992 "of chars is too long");
994 return init;
998 /* Handle scalar types (including conversions) and references. */
999 if ((TREE_CODE (type) != COMPLEX_TYPE
1000 || BRACE_ENCLOSED_INITIALIZER_P (init))
1001 && (SCALAR_TYPE_P (type) || code == REFERENCE_TYPE))
1003 tree *exp;
1005 if (nested)
1006 check_narrowing (type, init);
1007 init = convert_for_initialization (0, type, init, flags,
1008 ICR_INIT, NULL_TREE, 0,
1009 complain);
1010 exp = &init;
1012 /* Skip any conversions since we'll be outputting the underlying
1013 constant. */
1014 while (CONVERT_EXPR_P (*exp)
1015 || TREE_CODE (*exp) == NON_LVALUE_EXPR)
1016 exp = &TREE_OPERAND (*exp, 0);
1018 *exp = cplus_expand_constant (*exp);
1020 return init;
1023 /* Come here only for aggregates: records, arrays, unions, complex numbers
1024 and vectors. */
1025 gcc_assert (TREE_CODE (type) == ARRAY_TYPE
1026 || TREE_CODE (type) == VECTOR_TYPE
1027 || TREE_CODE (type) == RECORD_TYPE
1028 || TREE_CODE (type) == UNION_TYPE
1029 || TREE_CODE (type) == COMPLEX_TYPE);
1031 if (BRACE_ENCLOSED_INITIALIZER_P (init)
1032 && !TYPE_NON_AGGREGATE_CLASS (type))
1033 return process_init_constructor (type, init, complain);
1034 else
1036 if (COMPOUND_LITERAL_P (init) && TREE_CODE (type) == ARRAY_TYPE)
1038 if (complain & tf_error)
1039 error ("cannot initialize aggregate of type %qT with "
1040 "a compound literal", type);
1042 return error_mark_node;
1045 if (TREE_CODE (type) == ARRAY_TYPE
1046 && !BRACE_ENCLOSED_INITIALIZER_P (init))
1048 /* Allow the result of build_array_copy and of
1049 build_value_init_noctor. */
1050 if ((TREE_CODE (init) == VEC_INIT_EXPR
1051 || TREE_CODE (init) == CONSTRUCTOR)
1052 && (same_type_ignoring_top_level_qualifiers_p
1053 (type, TREE_TYPE (init))))
1054 return init;
1056 if (complain & tf_error)
1057 error ("array must be initialized with a brace-enclosed"
1058 " initializer");
1059 return error_mark_node;
1062 return convert_for_initialization (NULL_TREE, type, init,
1063 flags,
1064 ICR_INIT, NULL_TREE, 0,
1065 complain);
1069 tree
1070 digest_init (tree type, tree init, tsubst_flags_t complain)
1072 return digest_init_r (type, init, false, LOOKUP_IMPLICIT, complain);
1075 tree
1076 digest_init_flags (tree type, tree init, int flags)
1078 return digest_init_r (type, init, false, flags, tf_warning_or_error);
1081 /* Set of flags used within process_init_constructor to describe the
1082 initializers. */
1083 #define PICFLAG_ERRONEOUS 1
1084 #define PICFLAG_NOT_ALL_CONSTANT 2
1085 #define PICFLAG_NOT_ALL_SIMPLE 4
1087 /* Given an initializer INIT, return the flag (PICFLAG_*) which better
1088 describe it. */
1090 static int
1091 picflag_from_initializer (tree init)
1093 if (init == error_mark_node)
1094 return PICFLAG_ERRONEOUS;
1095 else if (!TREE_CONSTANT (init))
1096 return PICFLAG_NOT_ALL_CONSTANT;
1097 else if (!initializer_constant_valid_p (init, TREE_TYPE (init)))
1098 return PICFLAG_NOT_ALL_SIMPLE;
1099 return 0;
1102 /* Subroutine of process_init_constructor, which will process an initializer
1103 INIT for an array or vector of type TYPE. Returns the flags (PICFLAG_*)
1104 which describe the initializers. */
1106 static int
1107 process_init_constructor_array (tree type, tree init,
1108 tsubst_flags_t complain)
1110 unsigned HOST_WIDE_INT i, len = 0;
1111 int flags = 0;
1112 bool unbounded = false;
1113 constructor_elt *ce;
1114 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (init);
1116 gcc_assert (TREE_CODE (type) == ARRAY_TYPE
1117 || TREE_CODE (type) == VECTOR_TYPE);
1119 if (TREE_CODE (type) == ARRAY_TYPE)
1121 tree domain = TYPE_DOMAIN (type);
1122 if (domain && TREE_CONSTANT (TYPE_MAX_VALUE (domain)))
1123 len = (tree_to_double_int (TYPE_MAX_VALUE (domain))
1124 - tree_to_double_int (TYPE_MIN_VALUE (domain))
1125 + double_int_one)
1126 .ext (TYPE_PRECISION (TREE_TYPE (domain)),
1127 TYPE_UNSIGNED (TREE_TYPE (domain)))
1128 .low;
1129 else
1130 unbounded = true; /* Take as many as there are. */
1132 else
1133 /* Vectors are like simple fixed-size arrays. */
1134 len = TYPE_VECTOR_SUBPARTS (type);
1136 /* There must not be more initializers than needed. */
1137 if (!unbounded && vec_safe_length (v) > len)
1139 if (complain & tf_error)
1140 error ("too many initializers for %qT", type);
1141 else
1142 return PICFLAG_ERRONEOUS;
1145 FOR_EACH_VEC_SAFE_ELT (v, i, ce)
1147 if (ce->index)
1149 gcc_assert (TREE_CODE (ce->index) == INTEGER_CST);
1150 if (compare_tree_int (ce->index, i) != 0)
1152 ce->value = error_mark_node;
1153 sorry ("non-trivial designated initializers not supported");
1156 else
1157 ce->index = size_int (i);
1158 gcc_assert (ce->value);
1159 ce->value = digest_init_r (TREE_TYPE (type), ce->value, true,
1160 LOOKUP_IMPLICIT, complain);
1162 if (ce->value != error_mark_node)
1163 gcc_assert (same_type_ignoring_top_level_qualifiers_p
1164 (TREE_TYPE (type), TREE_TYPE (ce->value)));
1166 flags |= picflag_from_initializer (ce->value);
1169 /* No more initializers. If the array is unbounded, we are done. Otherwise,
1170 we must add initializers ourselves. */
1171 if (!unbounded)
1172 for (; i < len; ++i)
1174 tree next;
1176 if (type_build_ctor_call (TREE_TYPE (type)))
1178 /* If this type needs constructors run for default-initialization,
1179 we can't rely on the back end to do it for us, so make the
1180 initialization explicit by list-initializing from {}. */
1181 next = build_constructor (init_list_type_node, NULL);
1182 next = digest_init (TREE_TYPE (type), next, complain);
1184 else if (!zero_init_p (TREE_TYPE (type)))
1185 next = build_zero_init (TREE_TYPE (type),
1186 /*nelts=*/NULL_TREE,
1187 /*static_storage_p=*/false);
1188 else
1189 /* The default zero-initialization is fine for us; don't
1190 add anything to the CONSTRUCTOR. */
1191 break;
1193 flags |= picflag_from_initializer (next);
1194 CONSTRUCTOR_APPEND_ELT (v, size_int (i), next);
1197 CONSTRUCTOR_ELTS (init) = v;
1198 return flags;
1201 /* Subroutine of process_init_constructor, which will process an initializer
1202 INIT for a class of type TYPE. Returns the flags (PICFLAG_*) which describe
1203 the initializers. */
1205 static int
1206 process_init_constructor_record (tree type, tree init,
1207 tsubst_flags_t complain)
1209 vec<constructor_elt, va_gc> *v = NULL;
1210 int flags = 0;
1211 tree field;
1212 unsigned HOST_WIDE_INT idx = 0;
1214 gcc_assert (TREE_CODE (type) == RECORD_TYPE);
1215 gcc_assert (!CLASSTYPE_VBASECLASSES (type));
1216 gcc_assert (!TYPE_BINFO (type)
1217 || !BINFO_N_BASE_BINFOS (TYPE_BINFO (type)));
1218 gcc_assert (!TYPE_POLYMORPHIC_P (type));
1220 /* Generally, we will always have an index for each initializer (which is
1221 a FIELD_DECL, put by reshape_init), but compound literals don't go trough
1222 reshape_init. So we need to handle both cases. */
1223 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
1225 tree next;
1226 tree type;
1228 if (!DECL_NAME (field) && DECL_C_BIT_FIELD (field))
1230 flags |= picflag_from_initializer (integer_zero_node);
1231 CONSTRUCTOR_APPEND_ELT (v, field, integer_zero_node);
1232 continue;
1235 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
1236 continue;
1238 /* If this is a bitfield, first convert to the declared type. */
1239 type = TREE_TYPE (field);
1240 if (DECL_BIT_FIELD_TYPE (field))
1241 type = DECL_BIT_FIELD_TYPE (field);
1242 if (type == error_mark_node)
1243 return PICFLAG_ERRONEOUS;
1245 if (idx < vec_safe_length (CONSTRUCTOR_ELTS (init)))
1247 constructor_elt *ce = &(*CONSTRUCTOR_ELTS (init))[idx];
1248 if (ce->index)
1250 /* We can have either a FIELD_DECL or an IDENTIFIER_NODE. The
1251 latter case can happen in templates where lookup has to be
1252 deferred. */
1253 gcc_assert (TREE_CODE (ce->index) == FIELD_DECL
1254 || identifier_p (ce->index));
1255 if (ce->index != field
1256 && ce->index != DECL_NAME (field))
1258 ce->value = error_mark_node;
1259 sorry ("non-trivial designated initializers not supported");
1263 gcc_assert (ce->value);
1264 next = digest_init_r (type, ce->value, true,
1265 LOOKUP_IMPLICIT, complain);
1266 ++idx;
1268 else if (type_build_ctor_call (TREE_TYPE (field)))
1270 /* If this type needs constructors run for
1271 default-initialization, we can't rely on the back end to do it
1272 for us, so build up TARGET_EXPRs. If the type in question is
1273 a class, just build one up; if it's an array, recurse. */
1274 next = build_constructor (init_list_type_node, NULL);
1275 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (field)))
1277 next = finish_compound_literal (TREE_TYPE (field), next,
1278 complain);
1279 /* direct-initialize the target. No temporary is going
1280 to be involved. */
1281 if (TREE_CODE (next) == TARGET_EXPR)
1282 TARGET_EXPR_DIRECT_INIT_P (next) = true;
1285 next = digest_init_r (TREE_TYPE (field), next, true,
1286 LOOKUP_IMPLICIT, complain);
1288 /* Warn when some struct elements are implicitly initialized. */
1289 warning (OPT_Wmissing_field_initializers,
1290 "missing initializer for member %qD", field);
1292 else
1294 if (TREE_READONLY (field))
1296 if (complain & tf_error)
1297 error ("uninitialized const member %qD", field);
1298 else
1299 return PICFLAG_ERRONEOUS;
1301 else if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (TREE_TYPE (field)))
1303 if (complain & tf_error)
1304 error ("member %qD with uninitialized const fields", field);
1305 else
1306 return PICFLAG_ERRONEOUS;
1308 else if (TREE_CODE (TREE_TYPE (field)) == REFERENCE_TYPE)
1310 if (complain & tf_error)
1311 error ("member %qD is uninitialized reference", field);
1312 else
1313 return PICFLAG_ERRONEOUS;
1316 /* Warn when some struct elements are implicitly initialized
1317 to zero. */
1318 warning (OPT_Wmissing_field_initializers,
1319 "missing initializer for member %qD", field);
1321 if (!zero_init_p (TREE_TYPE (field)))
1322 next = build_zero_init (TREE_TYPE (field), /*nelts=*/NULL_TREE,
1323 /*static_storage_p=*/false);
1324 else
1325 /* The default zero-initialization is fine for us; don't
1326 add anything to the CONSTRUCTOR. */
1327 continue;
1330 /* If this is a bitfield, now convert to the lowered type. */
1331 if (type != TREE_TYPE (field))
1332 next = cp_convert_and_check (TREE_TYPE (field), next, complain);
1333 flags |= picflag_from_initializer (next);
1334 CONSTRUCTOR_APPEND_ELT (v, field, next);
1337 if (idx < vec_safe_length (CONSTRUCTOR_ELTS (init)))
1339 if (complain & tf_error)
1340 error ("too many initializers for %qT", type);
1341 else
1342 return PICFLAG_ERRONEOUS;
1345 CONSTRUCTOR_ELTS (init) = v;
1346 return flags;
1349 /* Subroutine of process_init_constructor, which will process a single
1350 initializer INIT for a union of type TYPE. Returns the flags (PICFLAG_*)
1351 which describe the initializer. */
1353 static int
1354 process_init_constructor_union (tree type, tree init,
1355 tsubst_flags_t complain)
1357 constructor_elt *ce;
1358 int len;
1360 /* If the initializer was empty, use default zero initialization. */
1361 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
1362 return 0;
1364 len = CONSTRUCTOR_ELTS (init)->length ();
1365 if (len > 1)
1367 if (!(complain & tf_error))
1368 return PICFLAG_ERRONEOUS;
1369 error ("too many initializers for %qT", type);
1370 CONSTRUCTOR_ELTS (init)->block_remove (1, len-1);
1373 ce = &(*CONSTRUCTOR_ELTS (init))[0];
1375 /* If this element specifies a field, initialize via that field. */
1376 if (ce->index)
1378 if (TREE_CODE (ce->index) == FIELD_DECL)
1380 else if (identifier_p (ce->index))
1382 /* This can happen within a cast, see g++.dg/opt/cse2.C. */
1383 tree name = ce->index;
1384 tree field;
1385 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1386 if (DECL_NAME (field) == name)
1387 break;
1388 if (!field)
1390 if (complain & tf_error)
1391 error ("no field %qD found in union being initialized",
1392 field);
1393 ce->value = error_mark_node;
1395 ce->index = field;
1397 else
1399 gcc_assert (TREE_CODE (ce->index) == INTEGER_CST
1400 || TREE_CODE (ce->index) == RANGE_EXPR);
1401 if (complain & tf_error)
1402 error ("index value instead of field name in union initializer");
1403 ce->value = error_mark_node;
1406 else
1408 /* Find the first named field. ANSI decided in September 1990
1409 that only named fields count here. */
1410 tree field = TYPE_FIELDS (type);
1411 while (field && (!DECL_NAME (field) || TREE_CODE (field) != FIELD_DECL))
1412 field = TREE_CHAIN (field);
1413 if (field == NULL_TREE)
1415 if (complain & tf_error)
1416 error ("too many initializers for %qT", type);
1417 ce->value = error_mark_node;
1419 ce->index = field;
1422 if (ce->value && ce->value != error_mark_node)
1423 ce->value = digest_init_r (TREE_TYPE (ce->index), ce->value,
1424 true, LOOKUP_IMPLICIT, complain);
1426 return picflag_from_initializer (ce->value);
1429 /* Process INIT, a constructor for a variable of aggregate type TYPE. The
1430 constructor is a brace-enclosed initializer, and will be modified in-place.
1432 Each element is converted to the right type through digest_init, and
1433 missing initializers are added following the language rules (zero-padding,
1434 etc.).
1436 After the execution, the initializer will have TREE_CONSTANT if all elts are
1437 constant, and TREE_STATIC set if, in addition, all elts are simple enough
1438 constants that the assembler and linker can compute them.
1440 The function returns the initializer itself, or error_mark_node in case
1441 of error. */
1443 static tree
1444 process_init_constructor (tree type, tree init, tsubst_flags_t complain)
1446 int flags;
1448 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
1450 if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == VECTOR_TYPE)
1451 flags = process_init_constructor_array (type, init, complain);
1452 else if (TREE_CODE (type) == RECORD_TYPE)
1453 flags = process_init_constructor_record (type, init, complain);
1454 else if (TREE_CODE (type) == UNION_TYPE)
1455 flags = process_init_constructor_union (type, init, complain);
1456 else
1457 gcc_unreachable ();
1459 if (flags & PICFLAG_ERRONEOUS)
1460 return error_mark_node;
1462 TREE_TYPE (init) = type;
1463 if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == NULL_TREE)
1464 cp_complete_array_type (&TREE_TYPE (init), init, /*do_default=*/0);
1465 if (flags & PICFLAG_NOT_ALL_CONSTANT)
1466 /* Make sure TREE_CONSTANT isn't set from build_constructor. */
1467 TREE_CONSTANT (init) = false;
1468 else
1470 TREE_CONSTANT (init) = 1;
1471 if (!(flags & PICFLAG_NOT_ALL_SIMPLE))
1472 TREE_STATIC (init) = 1;
1474 return init;
1477 /* Given a structure or union value DATUM, construct and return
1478 the structure or union component which results from narrowing
1479 that value to the base specified in BASETYPE. For example, given the
1480 hierarchy
1482 class L { int ii; };
1483 class A : L { ... };
1484 class B : L { ... };
1485 class C : A, B { ... };
1487 and the declaration
1489 C x;
1491 then the expression
1493 x.A::ii refers to the ii member of the L part of
1494 the A part of the C object named by X. In this case,
1495 DATUM would be x, and BASETYPE would be A.
1497 I used to think that this was nonconformant, that the standard specified
1498 that first we look up ii in A, then convert x to an L& and pull out the
1499 ii part. But in fact, it does say that we convert x to an A&; A here
1500 is known as the "naming class". (jason 2000-12-19)
1502 BINFO_P points to a variable initialized either to NULL_TREE or to the
1503 binfo for the specific base subobject we want to convert to. */
1505 tree
1506 build_scoped_ref (tree datum, tree basetype, tree* binfo_p)
1508 tree binfo;
1510 if (datum == error_mark_node)
1511 return error_mark_node;
1512 if (*binfo_p)
1513 binfo = *binfo_p;
1514 else
1515 binfo = lookup_base (TREE_TYPE (datum), basetype, ba_check,
1516 NULL, tf_warning_or_error);
1518 if (!binfo || binfo == error_mark_node)
1520 *binfo_p = NULL_TREE;
1521 if (!binfo)
1522 error_not_base_type (basetype, TREE_TYPE (datum));
1523 return error_mark_node;
1526 *binfo_p = binfo;
1527 return build_base_path (PLUS_EXPR, datum, binfo, 1,
1528 tf_warning_or_error);
1531 /* Build a reference to an object specified by the C++ `->' operator.
1532 Usually this just involves dereferencing the object, but if the
1533 `->' operator is overloaded, then such overloads must be
1534 performed until an object which does not have the `->' operator
1535 overloaded is found. An error is reported when circular pointer
1536 delegation is detected. */
1538 tree
1539 build_x_arrow (location_t loc, tree expr, tsubst_flags_t complain)
1541 tree orig_expr = expr;
1542 tree type = TREE_TYPE (expr);
1543 tree last_rval = NULL_TREE;
1544 vec<tree, va_gc> *types_memoized = NULL;
1546 if (type == error_mark_node)
1547 return error_mark_node;
1549 if (processing_template_decl)
1551 if (type_dependent_expression_p (expr))
1552 return build_min_nt_loc (loc, ARROW_EXPR, expr);
1553 expr = build_non_dependent_expr (expr);
1556 if (MAYBE_CLASS_TYPE_P (type))
1558 struct tinst_level *actual_inst = current_instantiation ();
1559 tree fn = NULL;
1561 while ((expr = build_new_op (loc, COMPONENT_REF,
1562 LOOKUP_NORMAL, expr, NULL_TREE, NULL_TREE,
1563 &fn, complain)))
1565 if (expr == error_mark_node)
1566 return error_mark_node;
1568 if (fn && DECL_USE_TEMPLATE (fn))
1569 push_tinst_level (fn);
1570 fn = NULL;
1572 if (vec_member (TREE_TYPE (expr), types_memoized))
1574 if (complain & tf_error)
1575 error ("circular pointer delegation detected");
1576 return error_mark_node;
1579 vec_safe_push (types_memoized, TREE_TYPE (expr));
1580 last_rval = expr;
1583 while (current_instantiation () != actual_inst)
1584 pop_tinst_level ();
1586 if (last_rval == NULL_TREE)
1588 if (complain & tf_error)
1589 error ("base operand of %<->%> has non-pointer type %qT", type);
1590 return error_mark_node;
1593 if (TREE_CODE (TREE_TYPE (last_rval)) == REFERENCE_TYPE)
1594 last_rval = convert_from_reference (last_rval);
1596 else
1597 last_rval = decay_conversion (expr, complain);
1599 if (TYPE_PTR_P (TREE_TYPE (last_rval)))
1601 if (processing_template_decl)
1603 expr = build_min (ARROW_EXPR, TREE_TYPE (TREE_TYPE (last_rval)),
1604 orig_expr);
1605 TREE_SIDE_EFFECTS (expr) = TREE_SIDE_EFFECTS (last_rval);
1606 return expr;
1609 return cp_build_indirect_ref (last_rval, RO_NULL, complain);
1612 if (complain & tf_error)
1614 if (types_memoized)
1615 error ("result of %<operator->()%> yields non-pointer result");
1616 else
1617 error ("base operand of %<->%> is not a pointer");
1619 return error_mark_node;
1622 /* Return an expression for "DATUM .* COMPONENT". DATUM has not
1623 already been checked out to be of aggregate type. */
1625 tree
1626 build_m_component_ref (tree datum, tree component, tsubst_flags_t complain)
1628 tree ptrmem_type;
1629 tree objtype;
1630 tree type;
1631 tree binfo;
1632 tree ctype;
1634 if (error_operand_p (datum) || error_operand_p (component))
1635 return error_mark_node;
1637 datum = mark_lvalue_use (datum);
1638 component = mark_rvalue_use (component);
1640 ptrmem_type = TREE_TYPE (component);
1641 if (!TYPE_PTRMEM_P (ptrmem_type))
1643 if (complain & tf_error)
1644 error ("%qE cannot be used as a member pointer, since it is of "
1645 "type %qT", component, ptrmem_type);
1646 return error_mark_node;
1649 objtype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
1650 if (! MAYBE_CLASS_TYPE_P (objtype))
1652 if (complain & tf_error)
1653 error ("cannot apply member pointer %qE to %qE, which is of "
1654 "non-class type %qT", component, datum, objtype);
1655 return error_mark_node;
1658 type = TYPE_PTRMEM_POINTED_TO_TYPE (ptrmem_type);
1659 ctype = complete_type (TYPE_PTRMEM_CLASS_TYPE (ptrmem_type));
1661 if (!COMPLETE_TYPE_P (ctype))
1663 if (!same_type_p (ctype, objtype))
1664 goto mismatch;
1665 binfo = NULL;
1667 else
1669 binfo = lookup_base (objtype, ctype, ba_check, NULL, complain);
1671 if (!binfo)
1673 mismatch:
1674 if (complain & tf_error)
1675 error ("pointer to member type %qT incompatible with object "
1676 "type %qT", type, objtype);
1677 return error_mark_node;
1679 else if (binfo == error_mark_node)
1680 return error_mark_node;
1683 if (TYPE_PTRDATAMEM_P (ptrmem_type))
1685 cp_lvalue_kind kind = lvalue_kind (datum);
1686 tree ptype;
1688 /* Compute the type of the field, as described in [expr.ref].
1689 There's no such thing as a mutable pointer-to-member, so
1690 things are not as complex as they are for references to
1691 non-static data members. */
1692 type = cp_build_qualified_type (type,
1693 (cp_type_quals (type)
1694 | cp_type_quals (TREE_TYPE (datum))));
1696 datum = build_address (datum);
1698 /* Convert object to the correct base. */
1699 if (binfo)
1701 datum = build_base_path (PLUS_EXPR, datum, binfo, 1, complain);
1702 if (datum == error_mark_node)
1703 return error_mark_node;
1706 /* Build an expression for "object + offset" where offset is the
1707 value stored in the pointer-to-data-member. */
1708 ptype = build_pointer_type (type);
1709 datum = fold_build_pointer_plus (fold_convert (ptype, datum), component);
1710 datum = cp_build_indirect_ref (datum, RO_NULL, complain);
1711 if (datum == error_mark_node)
1712 return error_mark_node;
1714 /* If the object expression was an rvalue, return an rvalue. */
1715 if (kind & clk_class)
1716 datum = rvalue (datum);
1717 else if (kind & clk_rvalueref)
1718 datum = move (datum);
1719 return datum;
1721 else
1723 /* 5.5/6: In a .* expression whose object expression is an rvalue, the
1724 program is ill-formed if the second operand is a pointer to member
1725 function with ref-qualifier &. In a .* expression whose object
1726 expression is an lvalue, the program is ill-formed if the second
1727 operand is a pointer to member function with ref-qualifier &&. */
1728 if (FUNCTION_REF_QUALIFIED (type))
1730 bool lval = real_lvalue_p (datum);
1731 if (lval && FUNCTION_RVALUE_QUALIFIED (type))
1733 if (complain & tf_error)
1734 error ("pointer-to-member-function type %qT requires an rvalue",
1735 ptrmem_type);
1736 return error_mark_node;
1738 else if (!lval && !FUNCTION_RVALUE_QUALIFIED (type))
1740 if (complain & tf_error)
1741 error ("pointer-to-member-function type %qT requires an lvalue",
1742 ptrmem_type);
1743 return error_mark_node;
1746 return build2 (OFFSET_REF, type, datum, component);
1750 /* Return a tree node for the expression TYPENAME '(' PARMS ')'. */
1752 tree
1753 build_functional_cast (tree exp, tree parms, tsubst_flags_t complain)
1755 /* This is either a call to a constructor,
1756 or a C cast in C++'s `functional' notation. */
1758 /* The type to which we are casting. */
1759 tree type;
1760 vec<tree, va_gc> *parmvec;
1762 if (error_operand_p (exp) || parms == error_mark_node)
1763 return error_mark_node;
1765 if (TREE_CODE (exp) == TYPE_DECL)
1767 type = TREE_TYPE (exp);
1769 if (complain & tf_warning
1770 && TREE_DEPRECATED (type)
1771 && DECL_ARTIFICIAL (exp))
1772 warn_deprecated_use (type, NULL_TREE);
1774 else
1775 type = exp;
1777 /* We need to check this explicitly, since value-initialization of
1778 arrays is allowed in other situations. */
1779 if (TREE_CODE (type) == ARRAY_TYPE)
1781 if (complain & tf_error)
1782 error ("functional cast to array type %qT", type);
1783 return error_mark_node;
1786 if (type_uses_auto (type))
1788 if (complain & tf_error)
1789 error ("invalid use of %<auto%>");
1790 return error_mark_node;
1793 if (processing_template_decl)
1795 tree t;
1797 /* Diagnose this even in a template. We could also try harder
1798 to give all the usual errors when the type and args are
1799 non-dependent... */
1800 if (TREE_CODE (type) == REFERENCE_TYPE && !parms)
1802 if (complain & tf_error)
1803 error ("invalid value-initialization of reference type");
1804 return error_mark_node;
1807 t = build_min (CAST_EXPR, type, parms);
1808 /* We don't know if it will or will not have side effects. */
1809 TREE_SIDE_EFFECTS (t) = 1;
1810 return t;
1813 if (! MAYBE_CLASS_TYPE_P (type))
1815 if (parms == NULL_TREE)
1817 if (VOID_TYPE_P (type))
1818 return void_zero_node;
1819 return build_value_init (cv_unqualified (type), complain);
1822 /* This must build a C cast. */
1823 parms = build_x_compound_expr_from_list (parms, ELK_FUNC_CAST, complain);
1824 return cp_build_c_cast (type, parms, complain);
1827 /* Prepare to evaluate as a call to a constructor. If this expression
1828 is actually used, for example,
1830 return X (arg1, arg2, ...);
1832 then the slot being initialized will be filled in. */
1834 if (!complete_type_or_maybe_complain (type, NULL_TREE, complain))
1835 return error_mark_node;
1836 if (abstract_virtuals_error_sfinae (ACU_CAST, type, complain))
1837 return error_mark_node;
1839 /* [expr.type.conv]
1841 If the expression list is a single-expression, the type
1842 conversion is equivalent (in definedness, and if defined in
1843 meaning) to the corresponding cast expression. */
1844 if (parms && TREE_CHAIN (parms) == NULL_TREE)
1845 return cp_build_c_cast (type, TREE_VALUE (parms), complain);
1847 /* [expr.type.conv]
1849 The expression T(), where T is a simple-type-specifier for a
1850 non-array complete object type or the (possibly cv-qualified)
1851 void type, creates an rvalue of the specified type, which is
1852 value-initialized. */
1854 if (parms == NULL_TREE)
1856 exp = build_value_init (type, complain);
1857 exp = get_target_expr_sfinae (exp, complain);
1858 return exp;
1861 /* Call the constructor. */
1862 parmvec = make_tree_vector ();
1863 for (; parms != NULL_TREE; parms = TREE_CHAIN (parms))
1864 vec_safe_push (parmvec, TREE_VALUE (parms));
1865 exp = build_special_member_call (NULL_TREE, complete_ctor_identifier,
1866 &parmvec, type, LOOKUP_NORMAL, complain);
1867 release_tree_vector (parmvec);
1869 if (exp == error_mark_node)
1870 return error_mark_node;
1872 return build_cplus_new (type, exp, complain);
1876 /* Add new exception specifier SPEC, to the LIST we currently have.
1877 If it's already in LIST then do nothing.
1878 Moan if it's bad and we're allowed to. COMPLAIN < 0 means we
1879 know what we're doing. */
1881 tree
1882 add_exception_specifier (tree list, tree spec, int complain)
1884 bool ok;
1885 tree core = spec;
1886 bool is_ptr;
1887 diagnostic_t diag_type = DK_UNSPECIFIED; /* none */
1889 if (spec == error_mark_node)
1890 return list;
1892 gcc_assert (spec && (!list || TREE_VALUE (list)));
1894 /* [except.spec] 1, type in an exception specifier shall not be
1895 incomplete, or pointer or ref to incomplete other than pointer
1896 to cv void. */
1897 is_ptr = TYPE_PTR_P (core);
1898 if (is_ptr || TREE_CODE (core) == REFERENCE_TYPE)
1899 core = TREE_TYPE (core);
1900 if (complain < 0)
1901 ok = true;
1902 else if (VOID_TYPE_P (core))
1903 ok = is_ptr;
1904 else if (TREE_CODE (core) == TEMPLATE_TYPE_PARM)
1905 ok = true;
1906 else if (processing_template_decl)
1907 ok = true;
1908 else
1910 ok = true;
1911 /* 15.4/1 says that types in an exception specifier must be complete,
1912 but it seems more reasonable to only require this on definitions
1913 and calls. So just give a pedwarn at this point; we will give an
1914 error later if we hit one of those two cases. */
1915 if (!COMPLETE_TYPE_P (complete_type (core)))
1916 diag_type = DK_PEDWARN; /* pedwarn */
1919 if (ok)
1921 tree probe;
1923 for (probe = list; probe; probe = TREE_CHAIN (probe))
1924 if (same_type_p (TREE_VALUE (probe), spec))
1925 break;
1926 if (!probe)
1927 list = tree_cons (NULL_TREE, spec, list);
1929 else
1930 diag_type = DK_ERROR; /* error */
1932 if (diag_type != DK_UNSPECIFIED
1933 && (complain & tf_warning_or_error))
1934 cxx_incomplete_type_diagnostic (NULL_TREE, core, diag_type);
1936 return list;
1939 /* Like nothrow_spec_p, but don't abort on deferred noexcept. */
1941 static bool
1942 nothrow_spec_p_uninst (const_tree spec)
1944 if (DEFERRED_NOEXCEPT_SPEC_P (spec))
1945 return false;
1946 return nothrow_spec_p (spec);
1949 /* Combine the two exceptions specifier lists LIST and ADD, and return
1950 their union. If FN is non-null, it's the source of ADD. */
1952 tree
1953 merge_exception_specifiers (tree list, tree add, tree fn)
1955 tree noex, orig_list;
1957 /* No exception-specifier or noexcept(false) are less strict than
1958 anything else. Prefer the newer variant (LIST). */
1959 if (!list || list == noexcept_false_spec)
1960 return list;
1961 else if (!add || add == noexcept_false_spec)
1962 return add;
1964 /* noexcept(true) and throw() are stricter than anything else.
1965 As above, prefer the more recent one (LIST). */
1966 if (nothrow_spec_p_uninst (add))
1967 return list;
1969 noex = TREE_PURPOSE (list);
1970 if (DEFERRED_NOEXCEPT_SPEC_P (add))
1972 /* If ADD is a deferred noexcept, we must have been called from
1973 process_subob_fn. For implicitly declared functions, we build up
1974 a list of functions to consider at instantiation time. */
1975 if (noex && operand_equal_p (noex, boolean_true_node, 0))
1976 noex = NULL_TREE;
1977 gcc_assert (fn && (!noex || is_overloaded_fn (noex)));
1978 noex = build_overload (fn, noex);
1980 else if (nothrow_spec_p_uninst (list))
1981 return add;
1982 else
1983 gcc_checking_assert (!TREE_PURPOSE (add)
1984 || cp_tree_equal (noex, TREE_PURPOSE (add)));
1986 /* Combine the dynamic-exception-specifiers, if any. */
1987 orig_list = list;
1988 for (; add && TREE_VALUE (add); add = TREE_CHAIN (add))
1990 tree spec = TREE_VALUE (add);
1991 tree probe;
1993 for (probe = orig_list; probe && TREE_VALUE (probe);
1994 probe = TREE_CHAIN (probe))
1995 if (same_type_p (TREE_VALUE (probe), spec))
1996 break;
1997 if (!probe)
1999 spec = build_tree_list (NULL_TREE, spec);
2000 TREE_CHAIN (spec) = list;
2001 list = spec;
2005 /* Keep the noexcept-specifier at the beginning of the list. */
2006 if (noex != TREE_PURPOSE (list))
2007 list = tree_cons (noex, TREE_VALUE (list), TREE_CHAIN (list));
2009 return list;
2012 /* Subroutine of build_call. Ensure that each of the types in the
2013 exception specification is complete. Technically, 15.4/1 says that
2014 they need to be complete when we see a declaration of the function,
2015 but we should be able to get away with only requiring this when the
2016 function is defined or called. See also add_exception_specifier. */
2018 void
2019 require_complete_eh_spec_types (tree fntype, tree decl)
2021 tree raises;
2022 /* Don't complain about calls to op new. */
2023 if (decl && DECL_ARTIFICIAL (decl))
2024 return;
2025 for (raises = TYPE_RAISES_EXCEPTIONS (fntype); raises;
2026 raises = TREE_CHAIN (raises))
2028 tree type = TREE_VALUE (raises);
2029 if (type && !COMPLETE_TYPE_P (type))
2031 if (decl)
2032 error
2033 ("call to function %qD which throws incomplete type %q#T",
2034 decl, type);
2035 else
2036 error ("call to function which throws incomplete type %q#T",
2037 decl);
2043 #include "gt-cp-typeck2.h"